加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

如何用js把字符串转化为整型

(2012-10-12 18:51:59)
标签:

parseint()

js把字符串转化为整型

把字符串转化为整型

it

分类: Javascript/JQuery
如何用js把字符串转化为整型

parseInt() 函数可解析一个字符串,并返回一个整数。
parseInt(string, radix)
参数 描述
string 必需。要被解析的字符串。
radix

可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。

如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16 为基数。

如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。


例:======================================================================
parseInt("10");                    //返回 10

parseInt("19",10); //返回 19 (10+9)
parseInt("11",2); //返回 3 (2+1)
parseInt("17",8); //返回 15 (8+7)
parseInt("1f",16); //返回 31 (16+15)
parseInt("010"); //未定:返回 10 或 8



=========================================================================

//电子票数量加1
function add_nums(){   
    var current_nums = document.getElementByIdx_x("current_nums").value;
    if(current_nums!=parseInt(current_nums,10)){
        alert("购票数必须是>=1的整数");
        return false;
    }
    if(current_nums <= 0){
        alert("购票数必须是>=1的整数");
        return false;
    }
    //alert(current_nums);
    current_nums = parseInt(current_nums,10) + 1;
    (jQuery)("#current_nums").val(current_nums);

}
//电子票数量减1
function reduce_nums(){   
    var current_nums = document.getElementByIdx_x("current_nums").value;
    if(current_nums!=parseInt(current_nums,10)){
        alert("购票数必须是>=1的整数");
        return false;
    }
    if(current_nums <= 0){
        alert("购票数必须是>=1的整数");
        return false;
    }
    //alert(current_nums);
    if(current_nums == 1){
        return    false;
    }
    current_nums = parseInt(current_nums,10) - 1;

    (jQuery)("#current_nums").val(current_nums);

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有