使用 JavaScript 表单"action"



html代码:

<section class="main">
<form class="form-4" name="form4" id="form4" method="POST">
    <p class="submit">
        <button type="submit" name="submit" onclick="show()"><i class="icon-thumbs-up icon-large"></i></button>                     
    </p>
</form>
</section>

js代码:

function show(){
      $.ajax({        
      type: "POST",
      url: "check_login_points.php",
      data: {test : JSON.stringify(arr)},
      success: function(data) {
                if(data == 0)
                {
                  alert("       SORRY :( n misplaced cue points.");
                }
                else if(data == 1)
                {
                  document.getElementById("form4").action = "http://localhost/profile_book/login_key.php";
                  //alert("WELCOME !!!");
                  //$("#form4").attr('action', 'http://localhost/profile_book/login_key.php');
                }
                else if(data == 2)
                {
                  alert("enter cue-points");
                } 
            }
        }); 
}

当ajax函数成功时,我试图将表单操作放在javascript中。但形式动作似乎不起作用。我们非常感谢您的建议。提前感谢!

由于异步的性质,您不能做您想做的事情。它需要被分解成多个部分。

首先,将按钮重命名为其他名称。你会遇到问题的。

<button type="submit" name="btnSubmit" />

第二,将表单提交绑定到jQuery,而不是内联事件。

$("#form4").on("submit", function(event) {
    //cancel the submission
    event.preventDefault();
    //call your logic
    show();
});

现在最后一件事是在设置操作后手动触发提交。

function show (){
      $.ajax({        
      type: "POST",
      url: "check_login_points.php",
      data: {test : JSON.stringify(arr)},
      success: function(data) {
                if(data == 0) {
                  alert("SORRY :( n misplaced cue points.");
                } else if(data == 1) {
                  $("#form4").attr("action", "http://localhost/profile_book/login_key.php")[0].submit();
                } else if(data == 2) {    
                  alert("enter cue-points");
                } 
            }
        }); 
}

在这里冒险。假设你真的只是在问为什么表格没有提交,在这种情况下,这是因为你错过了表格提交:

var form = document.getElementById("form4");
form.action = "http://localhost/profile_book/login_key.php";
form.submit()

最新更新