![](/img/trans.png)
[英]textarea value is empty or not - check with jquery not working, why
[英]How to check if a Textarea is empty in Javascript or jQuery?
如何檢查 textarea 是否不包含任何內容?
我嘗試使用此代碼:
if(document.getElementById("field").value ==null)
{
alert("debug");
document.getElementById("field").style.display ="none";
}
但它並沒有達到我的預期。 我希望它應該出現一個消息框“調試”並且不顯示文本區域。
我該如何解決這個問題?
你想檢查值是否是== ""
,而不是NULL
。
if(document.getElementById("field").value == '')
{
alert("debug");
document.getElementById("field").style.display ="none";
}
更新
一個工作示例
另一個使用 TRIM以防您想確保他們不張貼空格
TRIM() 的實現
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
您也可以使用以下 jQuery 來轉義空格。
if($("#YourTextAreaID").val().trim().length < 1)
{
alert("Please Enter Text...");
return;
}
試試這個:
if(document.getElementById("field").value == "")
{
alert("debug");
document.getElementById("field").style.display ="none";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.