我正在使用(或試圖)JQuery Validation與WebForms/HTML。jQuery驗證 - 使用點擊驗證,而不是提交
我有,基本上(簡化了真正的HTML,只顯示元素):
<input id="txtEmail"/>
<input id="txtTicketID"/>
<a id="create" href="#">Create a new ticket</a>
<a id="find" href="#">Find this ticket</a>
進行驗證/操作的JavaScript是在這裏:
$(function() {
$("#aspnetForm").validate({
rules: {
txtEmail: {
required: true,
email: true
,
messages: {
required: "* enter your e-mail",
email: "* invalid e-mail"
}
},
txtTicketID: {
required: true,
digits: true,
messages: {
required: "*",
digits: "* invalid ticket"
}
}
},
onfocusout: true,
onkeyup: true,
onsubmit: false,
debug: true
});
$("#create").click(function() {
if ($("#aspnetForm").valid()) {
var email = $("#txtEmail").val();
if (email != "")
window.location = "CreateTicket.aspx?email=" + email;
}
});
$("#find").click(function() {
if ($("#aspnetForm").valid()) {
var email = $("#txtEmail").val();
var ticketID = $("#txtTicketID").val();
if (email != "" && ticketID != "")
window.location = "DetailEditTicket.aspx?email=" + email + "&ticketID=" + ticketID;
}
});
});
它不工作。 。點擊鏈接上的valid()會始終返回true,即使這些字段爲空或錯誤。
當我輸入東西,模糊等等時,沒有任何東西使驗證發生。
您是否看到缺少這方面的內容?
Steerpike是正確的,此外,您可以簡化一點,用class =「required email」等標記您的輸入,而不是將它們添加爲上述規則。一旦你在窗體上調用驗證,插件就會選擇這些。 – lucas 2009-08-27 14:45:04