【问题标题】:jquery - how to prevent form from redirecting after client side validation occurs with invalid datajquery - 如何在客户端验证发生无效数据后防止表单重定向
【发布时间】:2012-08-09 04:49:28
【问题描述】:

我在表单上有一个转发器,并使用 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 事件后面的代码中重定向。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 只是一个猜测,但也许你应该使用 .submit() 而不是 .click()

标签: jquery submit postback return preventdefault


【解决方案1】:

在函数内部调用 document.ready 并不是一个好主意。您正在绑定到提交按钮的单击事件,我要做的是删除函数包装器并绑定到表单本身的提交事件,这样当验证发现错误时您只需返回 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;
            }

        });
    });

【讨论】:

  • 在提交表单的情况下,您不希望 preventDefault() 在顶部 - 因为如果表单有效,您希望它执行默认操作。
  • 调用位于脚本引用的顶部,如下所示。
  • 我也在按钮上试过了。 使用 OnClientClick(return ValidateBid() ) 也没有运气
  • @ahren 当然你是对的——我已经习惯了对输入进行客户端验证。 Ches187 我现在正在修改我的答案
  • 好的,我把它从一个函数中取出并添加了 evt.preventDefault();按照推荐到顶部,它仍然会重定向。还有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2012-09-19
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
相关资源
最近更新 更多