简体   繁体   English

使用javascript形成“动作”

[英]Form “action” using javascript

html code : 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 code : 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");
                } 
            }
        }); 
}

Am trying to put form action in javascript when an ajax function succeeds. 我试图在ajax函数成功时将表单操作放入javascript中。 But the form action doesn't seem to work. 但是表单操作似乎不起作用。 Suggestions are highly appreciated. 建议非常赞赏。 Thanks in advance ! 提前致谢 !

You can not do what you want to do because Asynchronous nature. 您不能做自己想做的事情,因为异步性。 It will need to be broken up into multiple parts. 它需要分为多个部分。

First thing, rename the button to something else. 首先,将按钮重命名为其他名称。 You are going to run into issues. 您将遇到问题。

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

second bind the form submission with jQuery, not inline events. 第二个将表单提交与jQuery绑定,而不是内联事件。

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

Now last thing is to manually trigger the submit after setting the action. 现在,最后一件事是在设置操作后手动触发提交。

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");
                } 
            }
        }); 
}

Going out on a limb here. 在这里走出去。 Going to assume you're really just asking why the form isn't submitting, in which case it's because you're missing the form-submit: 假设您实际上只是在问为什么不提交表单,在这种情况下,这是因为您缺少表单提交:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM