如何定义文本框值null = 0并且值= 1?用javascript

时间:2017-02-08 02:16:16

标签: javascript

对不起,我在JS上需要帮助 我想尝试制作一个文本框检查器,可以检查文本框的值。当它的值大于0时,其值为1,但是当它具有空值或0值时,它的值为= 0

这是我的代码

// JavaScript Document

    function check()
        {
            if (document.getElementById('s1').value=="0" 
             || document.getElementById('s1').value==undefined)
            {

                return 0;
                document.getElementById('avr').value = result;
            }
            return 1;
                document.getElementById('avr').value = result;

        }

1 个答案:

答案 0 :(得分:2)

您需要在返回前设置元素的值。 return语句终止函数的执行:

function check()
{
  if (document.getElementById('s1').value === '0' || document.getElementById('s1').value === '') {
    document.getElementById('avr').value = result;
    return 0;
  } else {
    document.getElementById('avr').value = result;
    return 1;
  }
}