简体   繁体   English

JavaScript在onClick函数中检查字符串是否为空

[英]JavaScript check if string is empty during onClick function

Can anyone advise me what is wrong with the empty String check in the JavaScript below. 谁能告诉我下面的JavaScript中的空String检查有什么问题。

It works fine if I remove the line of code which checks for empty String, but I would like to display "Nothing selected" if the selection is empty. 如果我删除检查空字符串的代码行,它会很好地工作,但是如果选择为空,我想显示“ Nothing selected”。

<html>
  <script language="JavaScript">
  <!--
  function update()
  {
    var selection = "";
    var empty = "";
    if (document.form1.TeaName.checked)    selection += "Tea<br>";
    if (document.form1.CoffeeName.checked) selection += "Coffee<br>";
    if (document.form1.EggsName.checked)   selection += "Eggs<br>";
    if (document.form1.BaconName.checked)  selection += "Bacon<br>";
    if (selection.equals(empty)) selection = "Nothing selected.";
    document.getElementById("currentSelection").innerHTML = selection;
  }
  -->
  </script>
  <body>
    <form name="form1">
      <div id="container" style="width:100%">
        <div id="left" style="float:left; width: 30%;">
          <h3>List 1</h3>
          <input type="checkbox" onClick="update()" name="TeaName"    value="Tea">Tea<br>
          <input type="checkbox" onClick="update()" name="CoffeeName" value="Coffee">Coffee<br>
        </div>
        <div id="middle" style="float:left; width: 30%;">
          <h3>List 2</h3>
          <input type="checkbox" onClick="update()" name="EggsName"  value="Eggs">Eggs<br>
          <input type="checkbox" onClick="update()" name="BaconName" value="Bacon">Bacon<br>
        </div>
        <div id="right" style="float:left; width: 30%;">
          <h3>Currently selected</h3>
          <p id="currentSelection">Please make a selection.</p>
        </div>
      </div>
    </form>
  </body>
</html>

Thank you for your time, 感谢您的时间,

James 詹姆士

There's no such method equals . 没有这样的方法equals Don't complicate things: 不要使事情复杂化:

if (selection.length === 0) selection = "Nothing selected.";

replace this row: 替换此行:

 if (selection.equals(empty)) selection = "Nothing selected.";

with this: 有了这个:

 if (selection == "" ) selection = "Nothing selected.";

尝试这个

if(!selection) selection="Nothing selected.";

There is no equals function in javascript, you either have to check with the length or a simple === operator javascript中没有equals函数,您必须检查长度或简单的===运算符

if (selection.length<= 0) selection = "Nothing selected.";

               (OR)

if(selection === "") selection  = "Nothing selected";

              (OR)

selection=(selection.length > 0) ? selection : "Nothing selected";

The function equals you have used should be related to some javascript library or framework. 您使用过的函数应该与某些javascript库或框架相关。

You can simply have default text and skip the last check. 您可以简单地使用默认文本并跳过最后的检查。

var selection = "Nothing selected."; //default text
// your rest of the code.

OR 要么

Last check can be done as 最后检查可以完成

selection = selection || "Nothing selected.";

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

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