简体   繁体   中英

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).

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.

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 . Anything else will evaluate to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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