jQuery:即使在处理数字时,我也必须使用parseInt(),为什么? [英] jquery: i have to use parseInt() even when deal with numbers, why?

查看:40
本文介绍了jQuery:即使在处理数字时,我也必须使用parseInt(),为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本

<select id="select1">
      <option value="1">1day</option>
      <option value="2">2day</option>
      <option value="3">3day</option>
    </select>

    <select id="select2">
      <option value="1">1day</option>
      <option value="2">2day</option>
      <option value="3">3day</option>
    </select>

和jquery

$("#select2").change(function() {
            var max_value = parseInt($("#select2 :selected").val());
            var min_value = parseInt($("#select1 :selected").val());
            if(max_value < min_value)
            {
              $("#select1").val($(this).val());
            }
        });

现在,无论如何我还是无法理解-如果选项元素的值是整数,为什么我必须使用parseInt()?在某些情况下,如果没有parseInt(),它将无法正常工作.

and now, what i can't understand anyway - if values of option elements are integer numbers, why i have to use parseInt()? in some cases it doesn't work without parseInt().

谢谢

推荐答案

表单字段值始终存储为字符串.它们是否看起来像整数无关紧要;他们是字符串.您需要先将它们转换为整数:)

Form field values are always stored as strings. Whether or not they look like integers is irrelevant; they're strings. You need to convert them to integers before treating them as such :)

这篇关于jQuery:即使在处理数字时,我也必须使用parseInt(),为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆