开发者

How to identify the ID or value of when the submit button is clicked

I am having trouble identifying the particular value or ID of a submit button after it has been clicked and submitted using AJAX.

If I place the following code as a global function, it pr开发者_JAVA技巧operly alerts the value of the button clicked:

$(":submit").live('click', function() {
alert($(this).val());
})

However, when I attempt to define the variable, I am unable to use that variable from within the success callback function:

$(":submit").live('click', function() {
var whichButton =  $(this).val();
})

...
$("#applicant-form").validate({
   function(form) {
      $(form).ajaxSubmit({
...
        success: alert(whichButton);

I have also tried placing the code in the submitHandler, but that doesn't work either.

In a somewhat related post, a user had suggested I place the following code:

$("#accordion .edit").click(function(){
window.lastButtonClicked = this;  
});

...
submitHandler: function(){
var index_origin = $(window.lastButtonClicked).attr("name");
}

But I was not able to get that to get the value of the button clicked (it said that the value was undefined).

Any suggestions?

UPDATE: It might help if I provide more information about why I need to know which button is pressed. I have two kinds of submit buttons for each form in a multi-part form. I would like to do different things based on which button was clicked.


$(":submit").live('click', function() {
var whichButton =  $(this).val();
})

The scope of whichbutton is inside of this anonymous function; you can't access it from elsewhere. A quick fix might be to declare whichbutton as a global variable but there's probably very few cases where you should do that. More context as to what it is you're trying to do would help, right now it just looks like you're trying to alert the button text on success after an ajax form submit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜