我希望检查一个表单是否存在空值,但是我不确定最好的方法是什么,因此我尝试了以下代码:
Javascript:
function checkform()
{
if (document.getElementById("promotioncode").value == "")
{
// something is wrong
alert('There is a problem with the first field');
return false;
}
return true;
}
HTML:
<form id="orderForm" onSubmit="return checkform()">
<input name="promotioncode" id="promotioncode" type="text" />
<input name="price" id="price" type="text" value="€ 15,00" readonly="readonly"/>
<input class="submit" type="submit" value="Submit"/>
</form>
有没有人有想法或更好的解决方案?
required
属性就会发挥作用... 此外,我建议使用 DOM 方法附加处理程序,而不是内联属性。 - Bergi