HTML 的 form 提交之前如何验证数值不为空? 为空的话提示用户并终止提交?

如题所述

第1个回答  2012-08-05
判断表单的value属性,如果为空就终止提交。
if(document.getElementById("username").value=="")
{
}
第2个回答  2012-08-05
<form id="myform" method="post" action="test-2.html" onsubmit="return check()">
用户名:<input type="text" id="uname">
<input type="submit" value="提交" id="sub">
<script>
function check()
{
var nameValue=document.getElementById("uname").value;
if (!nameValue)
{
return false;
}
return true;
}
</script>本回答被提问者和网友采纳
相似回答