jquery表单submit()在使用即兴功能阻止正常提交后无法在回调函数内工作

时间:2012-03-05 13:18:21

标签: jquery preventdefault form-submit impromptu

我正在显示密码提示,而不是在用户单击表单的提交按钮时提交表单。我希望在用户单击提示的“确定”按钮时提交表单。我正在使用jquery即兴插件(同时使用版本3.1和4.0.1)。我很匆忙,没有弄错我的代码或者我完全错过了什么。

这是我的代码 -

试用1
HTML部分

<form name="frmAddAdnetworkTemplate" id="frmAddAdnetworkTemplate" action="someAction">
  ...
  ...
  <input id="submit" type="submit" name="submit" value="Submit"  onclick="return promptPassword();" />
</form>

Javascript部分

function promptPassword()
{
   /*prepared passwordForm = some html; here */
   $.prompt(passwordForm,{ buttons: { Ok:, Cancel: false , submit: }, callback: submitPasswordPrompt, focus: 1});

   return false; //so as to not submit the form
}

function submitPasswordPrompt(value,m,form)
{
    $("form#frmAddAdnetworkTemplate").submit(); //this does not work - no js error as well
}

但是,表格不提交。

试用版1.1 而不是在提交时调用submitPasswordPrompt,

function promptPassword()
{
   $.prompt(passwordForm,{ buttons: 
                             { Ok: $("#frmAddAdnetworkTemplate").submit(), //this too does not work
                               Cancel: false }, 
                           focus: 1
                          });

}

试验1.2

我尝试使用preventDefault() -

HTML部分

<input id="submit" type="submit" name="submit" value="Submit" onclick="promptPassword(event);"/>

Javascript部分

function promptPassword(e)
{
  e.preventDefault();
  $.prompt(passwordForm,{ buttons: { Ok: true, Cancel: false }, submit: submitPasswordPrompt});

  function submitPasswordPromptTest(e, value,m,form)
  {
     if(value)
     {
        $("#frmAddAdnetworkTemplate").submit(); //does not work
     }
  }

试用2 我还尝试通过绑定提交按钮上的click事件来调用文档文档.ready中的$ .prompt -

HTML部分

<input id="submit" type="submit" name="submit" value="Submit" />

Javascript部分

$("#submit").click(function(){
   $.prompt(passwordForm,{ buttons: { Ok: $("#frmAddAdnetworkTemplate").submit(), Cancel: false }});
   return false;
});

当我尝试$(“#frmAddAdnetworkTemplate”)时出现此错误.off('submit')。submit(); -

e[h] is not a function

enter image description here

3 个答案:

答案 0 :(得分:1)

将内联Javascript与jQuery函数和事件混合使其难以理解。

您的“试验1.2”尝试不起作用,因为onclick="promptPassword(event);"中没有定义“事件”变量,promptPassword()函数不接受任何参数。因此,您无法使用e.preventDefault(),因为e未定义(作为jQuery对象)。

但是,使用jQuery,您可以绑定到表单submit事件以捕获所有类型的提交。即使用Enter键或单击提交按钮时。

$(document).ready(function() {
    $('#myForm').submit(function(event) {
        var password = "password",
            promptText = "Password required",
            isAuthenticated = (password == prompt(promptText));

        if (!isAuthenticated) {
            event.preventDefault();
            //console.log("Invalid password");
        }
    });

});

查看 demo

答案 1 :(得分:1)

问题是由id =“submit”引起的 出于某种原因,这与HTML提交混淆了。之前我遇到过这个问题,答案是将id和名称(为了安全起见)更改为其他内容:

<input id="my-form-submit" type="submit" name="my-form-submit" value="Submit" />

答案 2 :(得分:1)

将您的按钮ID与提交

一起使用

for my senario

$.prompt(msg, {
    buttons: btns,
    submit: function (v) {
        //this is simple pre submit validation, the submit function
        //return true to proceed to the callback, or false to take 
        //no further action, the prompt will stay open.
    },
    callback: function (e, v, m, f) {
        if (v) {
            $("#MainContent_HiddenFieldIsContinueWithSave").val('1');
            $("#MainContent_buttonSave").submit();
        }
        else { }
    }
});