您好我想查一個形式,如果輸入的值是空的,但我不知道什麼做到這一點的最好辦法是,所以我想這:JS形式檢查空
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>
有沒有人有想法或某種更好的解決方案? thx的任何幫助:)
還有其他的方法可以做到這一點,但你已經有最好的辦法 – Krishna
這應該工作,並且不會讓表單被提交,如果該字段爲空。請注意,這往往會讓那些在現場輸入亂碼以繞過規則的人感到挫敗。當服務器獲得數據時,這會在數據中留下一個非空白的,無關緊要的值。從長遠來看,它可能會弄亂報告和其他處理。 –
既然你標記了這個[標籤:HTML5],'required'屬性應該這樣做......除此之外,我會建議附加處理程序與DOM方法,而不是內聯屬性。 – Bergi