透過jQuery處理表單驗證
想要對表單進行驗證只需要使用jQuery減少許多語法,使用前必須先加入jQuery
function doSend()
{
//alert($("input[@type=radio][@checked]").val());
var strMsg = "";
if ($("#drpProClass").val() == "請選擇") strMsg += $("#drpProClass").attr("check") + "\n\r";
if ($("#txtProName").val().length == 0) strMsg += $("#txtProName").attr("check") + "\n\r";
//驗證radio是否選取
if ($("input[@type=radio][@checked]").val() == "radDirect")
{
if ($("#txtProCount").val().length == 0) strMsg += $("#txtProCount").attr("check") + "\n\r";
if ($("#txtDirPrice").val().length == 0) strMsg += $("#txtDirPrice").attr("check") + "\n\r";
}
else if ($("input[@type=radio][@checked]").val() == "radUndirect")
{
if ($("#txtStartPrice").val().length == 0) strMsg += $("#txtStartPrice").attr("check") + "\n\r";
if ($("#txtBasePrice").val().length == 0) strMsg += $("#txtBasePrice").attr("check") + "\n\r";
}
if ($("#txtPoStDay").val().length == 0) strMsg += $("#txtPoStDay").attr("check") + "\n\r";
if ($("#txtPoEnDay").val().length == 0) strMsg += $("#txtPoEnDay").attr("check") + "\n\r";
if ($("#txtContent").val().length == 0) strMsg += $("#txtContent").attr("check") + "\n\r";
if (strMsg != "")
{
alert("下列欄位不得為空\n\r" + strMsg);
}
else {
if (document.all) {
document.getElementById("btnOK").click();
}
else
{
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
document.getElementById("btnOK").dispatchEvent(evt);
}
}
}
留言
張貼留言
您好,我是 Lawrence,這裡是我的開發筆記的網誌,如果你對我的文章有任何疑問或者有錯誤的話,歡迎留言讓我知道。