[英]check whether a textarea is empty
誰能告訴我這段代碼有什么問題:
function c(id)
{
var empty = document.getElementById(id);
if(empty.length<1)
{
window.alert ("This field cant be left empty");
return true;
}
else
{
return false;
}
}
這是我的HTML代碼:
<textarea rows="3" cols="80" id="ta1" onChange="c('ta1');"></textarea>
應檢查textarea的value屬性以確定它是否為空。
var content = document.getElementById(id).value;
if(content.length<1)
{
window.alert ("This field cant be left empty");
return true;
}
else
{
return false;
}
工作示例: http : //jsfiddle.net/35DFR/2/
if (YOURFORM.YOURTEXTFIELDVARIABLENAME.value == "")
{
return True
}
嘗試這個:
function c(id)
{
if(document.getElementById(id).value == '')
{
window.alert ("This field cant be left empty");
return true;
}
else
{
return false;
}
}
如果你想更進一步,你可能想要先修剪該值。
更新:
從評論中,嘗試將' onchange '更改為' onkeyup ':
<textarea rows="3" cols="80" id="ta1" onkeyup="c('ta1');"></textarea>
function c(id) {
var empty =document.getElementById(id);
if(!empty.value){
window.alert("This field cant be left empty");
return true;
}else{
return false;
}
}
嘗試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.