js常用代码片段

禁止浏览器返回

<script language="javascript">
    //防止页面后退
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', function () {
            history.pushState(null, null, document.URL);
    });
</script>
View Code

返回上一页并刷新

self.location = document.referrer;
View Code

对象转JSON字符串

JSON.stringify(model)
View Code

JSON字符串转对象

var obj = JSON.parse(str); //由JSON字符串转换为JSON对象
View Code

无痕迹跳转

window.location.replace('/Admin/Auth/LockScreen')
View Code

 获取url指定参数名的值

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var paramValue = window.location.search.substr(1).match(reg);
    console.info("paramValue:" + paramValue);
    if (paramValue != null) return unescape(paramValue[2]); return null;
}
View Code

 回车事件

    document.onkeyup = function (e) {
        var code = e.charCode || e.keyCode;  
        if (code == 13) {
            $.login($("#SUB"));
        }
    }
View Code

 判断checkbox是否被选中

if($('#checkbox-id').is(':checked')) {
    // do something
}
View Code

 刷新当前页面

 location.reload() 
View Code

 获取单选按钮rido选中的值

$("input:radio[name=Type]:checked").val()
View Code

 文本框输入检查

$("input[name='quantity']").bind("propertychange input", function () {
        var numChange = $(this).val();
        numChange = parseInt(numChange);
        console.info(numChange);
        if (isNaN(numChange)) {
            $(this).val(1)
        } else {
            $(this).val(numChange)

            if (numChange < 1) {
                $(this).val(1)
            }
        }
    });
View Code

js编码和解码

 window.location.href = "/Query/BrandList?cxkey=" +escape( title);
View Code

 文本框输入正整数

        $(".integer").each(function () {
            $(this).bind("propertychange input", function () {
                var numChange = $(this).val();
                numChange = parseInt(numChange);
                console.info(numChange);
                if (isNaN(numChange)) {
                    $(this).val(1)
                } else {
                    $(this).val(numChange)

                    if (numChange < 1) {
                        $(this).val(1)
                    }
                }
            });
        });
View Code

 

转载于:https://www.cnblogs.com/liandy0906/p/7795567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值