简体   繁体   English

JS检查值是否存在

[英]JS check if value exists

For some reason my JS is not reacting as I thought it would. 由于某种原因,我的JS没有按照我的预期做出反应。 The submit button is disabled at page load. 页面加载时,提交按钮被禁用。

When en value is entered in the datum field the class should change and submit button should enabled. 在基准字段中输入en值后,该类应更改,并且应该启用提交按钮。 Neither is happening. 都没有发生。

It seems an standard code but still an issue? 似乎是标准代码,但还是有问题?

 window.onload = function() { document.getElementById('form_submit').disabled = true; } function validate() { if (document.getElementById('datum').value == false) { document.getElementById('div_datum').className = "col-md-2 input-group has-warning"; } else { document.getElementById('div_datum').className = "col-md-2 input-group has-success"; document.getElementById('form_submit').disabled = false; } } 
 <div class="container"> <div class="form-group row"> <label for="datum" class="col-md-2 form-label">Datum toolbox</label> <div class="col-md-2 input-group has-warning" id="div_datum"> <input type="text" class="form-control datepicker" id="datum" name="datum" onkeyup="validate()" onclick="validate()"><span class="input-group-addon"><span class="glyphicon glyphicon-remove" aria-hidden="true" data-toggle="tooltip" title="Verplicht veld"></span></span> </div> </div> <button type="submit" class="btn btn-primary" id="form_submit">Verstuur</button> </div> 

This line: 这行:

if (document.getElementById('datum').value == false)

will branch into the attached if block on a couple of values (for instance, if I type 0, not just when the value is blank (because "0" == false is, bizarrely, true ). A more reliable check for a blank value is: 将在几个值上分支到if块(例如,如果我键入0,而不仅仅是在值是空的时候(因为"0" == false ,奇怪的是, true )。值是:

if (!document.getElementById('datum').value)

That will branch in only if value is "" . 仅当value""该分支才会分支。 (You may or may not want to add .trim() to value if you want to clear leading and trailing whitespace.) (如果要清除.trim()和结尾的空白,则可能会或可能不想在value上添加.trim() 。)

Separately, I can't help but notice that your validate function never sets disabled to true , only false . 另外,我不禁要注意,您的validate函数从不会将disabled设置为true ,而只能将false设置为false What if I type something, then backspace over it? 如果我键入内容,然后在其上退格怎么办? You probably want to disable the button again. 您可能想再次禁用该按钮。 So: 所以:

function validate() {
  if (document.getElementById('datum').value == false) {
    document.getElementById('div_datum').className = "col-md-2 input-group has-warning";
    document.getElementById('form_submit').disabled = true;
  } else {
    document.getElementById('div_datum').className = "col-md-2 input-group has-success";
    document.getElementById('form_submit').disabled = false;
  }
}

or a bit more refactoring: 或更多重构:

function validate() {
  var invalid = !document.getElementById('datum').value.trim();
  document.getElementById('div_datum').className = "col-md-2 input-group " + (invalid ? "has-warning" : "has-success");
  document.getElementById('form_submit').disabled = invalid;
}

Note that String.prototype.trim was added in ES 5.1 (June 2011), so it doesn't exist in obsolete browsers like IE8, although it can be polyfilled; 请注意, String.prototype.trim是在ES 5.1(2011年6月)中添加的,因此它可以在诸如IE8之类的过时浏览器中不存在,尽管它可以被填充。 see the link for a polyfill. 请参见链接以获取polyfill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM