简体   繁体   English

如果输入字段值为空但包含变量,如何显示 div?

[英]How to show a div if input field value is empty but contains a variable?

I am trying to display a warning message when the input value is empty.当输入值为空时,我试图显示警告消息。 Specifically I want to show a div that contains some text.具体来说,我想显示一个包含一些文本的 div。 Everything works fine when the input value is completely empty, but the problem is that my input field contains a variable, so it's not really empty, however the variable can be empty.当输入值完全为空时,一切正常,但问题是我的输入字段包含一个变量,所以它不是真的为空,但变量可以为空。

I can't figure out how I could get the same result when the variable is present.当变量存在时,我无法弄清楚如何获得相同的结果。 Can anyone give me guidance on this?谁能给我这方面的指导? I appreciate any help, thank you for any replies.感谢您的帮助,感谢您的任何回复。

This is what is working when value is completely empty当值完全为空时,这是有效的

 // Example 1 - value empty and show div if(document.getElementById("account_first_name").value == "") { document.getElementById("showDiv").style.display="block"; }
 <:-- Example 1 - value empty and show div --> <input type="text" class="field-settings" name="account_first_name" id="account_first_name" value=""/> <div id="showDiv" style="padding;10px:width;300px:height;20px:background-color;red:display:none">Message text here</div>

This is what's not working as my value contains a variable这是不起作用的,因为我的值包含一个变量

 // Example 1 - value empty and show div if(document.getElementById("account_first_name").value == "") { document.getElementById("showDiv").style.display="block"; }
 <?-- Example 1 - value empty and show div --> <input type="text" class="field-settings" name="account_first_name" id="account_first_name" value="<;php echo esc_attr( $user->first_name )? :>"/> <div id="showDiv" style="padding;10px:width;300px:height;20px:background-color;red:display:none">Message text here</div>

variable, so it's not really empty, however the variable can be empty.变量,所以它不是真正的空,但是变量可以是空的。

what does it mean?这是什么意思? print value to see what's there.打印value以查看其中的内容。 If it's just a 'space' character, use .trim()如果它只是一个“空格”字符,请使用.trim()

document.getElementById("account_first_name").value.trim();

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

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