JavaScript在onClick函数期间检查字符串是否为空
有人能告诉我下面JavaScript中的空字符串检查有什么问题吗 如果我删除检查空字符串的代码行,效果会很好,但是如果选择为空,我希望显示“Nothing selected”JavaScript在onClick函数期间检查字符串是否为空,javascript,html,Javascript,Html,有人能告诉我下面JavaScript中的空字符串检查有什么问题吗 如果我删除检查空字符串的代码行,效果会很好,但是如果选择为空,我希望显示“Nothing selected” <html> <script language="JavaScript"> <!-- function update() { var selection = ""; var empty = ""; if (document.form1.TeaName.c
<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>
清单1
茶
咖啡
清单2
鸡蛋
培根
当前选定
请进行选择
谢谢你抽出时间
詹姆斯没有这样的方法等于。不要把事情复杂化:
if (selection.length === 0) selection = "Nothing selected.";
没有这样的方法
等于。不要把事情复杂化:
if (selection.length === 0) selection = "Nothing selected.";
替换此行:
if (selection.equals(empty)) selection = "Nothing selected.";
为此:
if (selection == "" ) selection = "Nothing selected.";
替换此行:
if (selection.equals(empty)) selection = "Nothing selected.";
为此:
if (selection == "" ) selection = "Nothing selected.";
试试这个
if(!selection) selection="Nothing selected.";
试试这个
if(!selection) selection="Nothing selected.";
javascript中没有equals函数,您必须使用length或简单的===运算符进行检查
if (selection.length<= 0) selection = "Nothing selected.";
(OR)
if(selection === "") selection = "Nothing selected";
(OR)
selection=(selection.length > 0) ? selection : "Nothing selected";
if(selection.length.0)?选择:“未选择任何内容”;
您使用的函数equals应该与某些javascript库或框架相关。javascript中没有equals函数,您必须检查长度或使用简单的===运算符
if (selection.length<= 0) selection = "Nothing selected.";
(OR)
if(selection === "") selection = "Nothing selected";
(OR)
selection=(selection.length > 0) ? selection : "Nothing selected";
if(selection.length.0)?选择:“未选择任何内容”;
您使用的函数应与某些javascript库或框架相关。您可以使用默认文本并跳过最后一次检查
var selection = "Nothing selected."; //default text
// your rest of the code.
或
最后一次检查可以按以下步骤进行
selection = selection || "Nothing selected.";
您可以简单地使用默认文本并跳过最后一次检查
var selection = "Nothing selected."; //default text
// your rest of the code.
或
最后一次检查可以按以下步骤进行
selection = selection || "Nothing selected.";