0

我只是想获取已提交表单的操作链接,但单击未定义的提交警报

$(document).on("submit","form",function(e){
     e.preventDefault();
     if($(this).children("input[type=submit]").hasClass("load") && $(this).children("input[type=submit]").hasClass("last-button"))
     {
        setTimeout(function(){
        alert($(this).attr("action"));
        //      $.post($(this).attr('action') , $(this).serialize() , function(data){
        //  $(".wholepage").html(data); 
        //  });
        },400);
     }
 });

HTML

<div class="well login">
    <form id="reg" action="parts/background/regProcess.php" method="post">
         <input placeholder="Choose an username" type="text" name="user"><br/><br/>
         <input placeholder="Choose a password" type="password" name="pass1"><br/><br/>
         <input placeholder="Input the password again" type="password" name="pass2"><br/><br/>
         <input placeholder="Enter your E-mail address" type="text" name="email"><br/>
         <input type="submit" class="last-button load" value="SUBMIT">   
    </form>
</div>
4

2 回答 2

1

Another solution is to bind the this using bind.

$(document).on("submit","form",function(e){
     e.preventDefault();
     if($(this).children("input[type=submit]").hasClass("load") && $(this).children("input[type=submit]").hasClass("last-button"))
     {
        setTimeout((function(){
        alert($(this).attr("action"));
        //      $.post($(this).attr('action') , $(this).serialize() , function(data){
        //  $(".wholepage").html(data); 
        //  });
        }).bind(this),400);
     }
 });

This would create a more maintainable code as the function is loosely tight to the parent function's variable.

于 2013-08-03T02:47:43.827 回答
1

您需要$(this)在超时功能之前存储:

$(document).on("submit","form",function(e){
  e.preventDefault();
  var $this = $(this);
  if($(this).children("input[type=submit]").hasClass("load") && $(this).children("input[type=submit]").hasClass("last-button"))
  {
    setTimeout(function(){
      var relPath = $this.attr("action");
      var absPath = $this.prop("action");
      alert(relPath);
      //      $.post($(this).attr('action') , $(this).serialize() , function(data){
      //  $(".wholepage").html(data); 
      //  });
    },400);
  }
});

这是一个小提琴:http: //jsfiddle.net/vMbHS/

于 2013-08-03T02:30:31.817 回答