2013-08-03 3 views
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> 
+0

Для работы с [CodePen] (http://codepen.io/anon/pen/GHapf). – FakeRainBrigand

ответ

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

Вот a скрипка: http://jsfiddle.net/vMbHS/

+0

FakeRainBrigand прокомментировал 'prop()', о котором я всегда забыл, поэтому добавил его. Комментарий, похоже, исчез. –

+0

это сработало спасибо :-) – Ario

1

Другим решением является связывание this с использованием 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); 
    } 
}); 

Это создало бы более удобный код, поскольку функция слабо привязана к переменной родительской функции.

Смежные вопросы