简体   繁体   English

Javascript:如果文字输入有任何价值

[英]Javascript: if text input has any value

The snippet of code below does "something" if a particular checkbox in a form is checked and a particular text input is empty. 如果选中了表单中的特定复选框并且特定文本输入为空,则下面的代码片段将执行“某些操作”。

else if (myform.mycheckbox[1].checked && myform.mytextinput.value=="")

How can I modify it so that it does something if the checkbox is checked and the text input has ANY value (the user inserted something in it). 我如何修改它,以便如果选中此复选框并且文本输入具有ANY值(用户在其中插入了内容),它可以执行某些操作。

Thank you, sorry if this is a very basic question.. 谢谢,很抱歉,如果这是一个非常基本的问题。

您将需要<>或在这种情况下!

else if (myform.mycheckbox[1].checked && myform.mytextinput.value!="")

You can do this: 你可以这样做:

else if (myform.mycheckbox[1].checked && myform.mytextinput.value)

That expression would evaluate to true if the checkbox is checked and there is something in the input field. 如果选中此复选框并且输入字段中有内容,则该表达式的计算结果将为true。

When you do a boolean evaluation on the content of a given variable it will evaluate to false if the content is any of the following: false , "" (empty string), 0 , null or undefined . 对给定变量的内容进行布尔评估时,如果内容为以下任意一项,则评估为falsefalse"" (空字符串), 0nullundefined Anything else will evaluate to true . 其他任何事情都将评估为true

if (myform.mycheckbox[1].checked && myform.mytextinput.value!=="")

If I understand you correctly, this would work: 如果我对您的理解正确,这将起作用:

else if (myform.mycheckbox[1].checked && myform.mytextinput.value.length > 0)

That would work as long as there was at least 1 character in the input. 只要输入中至少包含1个字符,该命令就可以工作。

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

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