如何在不提交表单的情况下重新启用PreventDefault

时间:2017-02-20 23:17:38

标签: javascript php jquery html forms

我正在使用带有4个不同按钮的表单。

第1步:输入几个表单字段

第2步:点击运行代码按钮

步骤3:Ajax处理第三个表单字段的值

第4步:点击“保存”按钮并提交表单。

问题是当我点击“运行代码”按钮时,效果很好。但是因为preventDefault打开,点击第二个按钮(保存)只需用一堆HTML代码重新填充第三个表单字段的值。

相反,我希望它提交。

这是我的代码。

JS

$(document).ready(function() {  

    $("#run-code").click(function()
    {
        $("#ajaxform").submit(function(e)
        {

            $("#SimpleLoader").html("<img src='images/loading.gif'/>");
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            $.ajax(
            {
                url : formURL,
                type: "POST",
                data : postData,
                success:function(data, textStatus, jqXHR) 
                {
                    $("#EmailAddress").val(data);
                    $("#run-code").attr('disabled','disabled');
                    $("#save").removeAttr('disabled');

                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                    $("#EmailAddress").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
                }
            });
            e.preventDefault(); //STOP default action
            e.unbind(e);

        });
            $("#ajaxform").submit(); //SUBMIT FORM
    });
});

HTML表单

<form name="ajaxform" id="ajaxform" action="inc/scripts/email_api.php" method="POST"  novalidate>

<tr>
<td>
    <input type="text" name="fname" placeholder="First Name" id="fname" required />
</td>
<td>
    <input type="text" name="lname" placeholder="Last Name" id="lname" required/>       
</td>
<td>
    <input type="email" name="email" value="" id="EmailAddress" placeholder="Email Address">
</td>
<td>
    <button id="run-code" name="getEmail" onclick="return validateForm()" >Run Code</button>
</td>
<td>
    <input type="submit" name="save" value="save" onclick='this.form.action="<?php echo $_SERVER['PHP_SELF'];?>"' id="save" disabled />
</td>
</form>

0 个答案:

没有答案