我在表单上有一个转发器,并使用jquery来验证转发器中的数据。我无法使用jquery阻止页面在客户端重定向。
这是我的jquery:
function ValidateBid()
{
$(document).ready(function () {
$(".btnSubmitBid").click(function (evt) {
var msg = "";
var bid = $(this).closest('td').find("input"); //this was the fun part
if (isNaN(bid.val())) {
msg = "Bid amount allowed in complete dollars only";
}
if (bid.val().indexOf('.') >= 0) {
msg = "Bid amount may only contain numbers";
}
if (bid.val() > 999999) {
msg = "If you want to place a bid for $" + bid.val() + " please contact customer service";
}
if (msg.length > 0) {
$('#dialogText').text(msg);
$('#dialog').dialog({
closeOnEscape: true, modal: true, width: 450, height: 200, title: 'Please correct the errors below:', close: function (event, ui) { $(this).dialog("destroy"); }
});
evt.preventDefault();
return false;
}
else {
return true;
}
});
});
} //ends doc rdy
我尝试过使用return false和evt.preventDefault();没有运气。
如果用户尚未登录,它将被重定向到repeater_ItemCommand事件后面的代码中。
非常感谢任何帮助。感谢。
答案 0 :(得分:2)
在函数内部调用document.ready不是一个好主意。你绑定到提交按钮的click事件,我要做的是删除函数包装器并绑定到表单本身的submit事件,这样你只需在验证发现错误时返回false,如果没有,它将继续如果您觉得有需要,那么返回true就不会造成任何伤害:
$(document).ready(function () {
$("form").submit(function (evt) {
//obv you would need to put the form id or class if you have more than one form on the page
var msg = "";
var bid = $(this).closest('td').find("input"); //this was the fun part
if (isNaN(bid.val())) {
msg = "Bid amount allowed in complete dollars only";
}
if (bid.val().indexOf('.') >= 0) {
msg = "Bid amount may only contain numbers";
}
if (bid.val() > 999999) {
msg = "If you want to place a bid for $" + bid.val() + " please contact customer service";
}
if (msg.length > 0) {
$('#dialogText').text(msg);
$('#dialog').dialog({
closeOnEscape: true, modal: true, width: 450, height: 200, title: 'Please correct the errors below:', close: function (event, ui) { $(this).dialog("destroy"); }
});
return false;
}
else {
return true;
}
});
});