简体   繁体   中英

i want to change value of a submit type button?

i have code of a form. Code for submit button:

<input type="submit" id="submit" value="Save" />

After successfully saved this form, i want to change value of button to "Update". I am using following code but it is not working.

$("#submit").click(function(){
$("#submit").val("Update");
});

More than likely, when you press the submit button, you are leaving the page and then reloading it.

You need to prevent the default behavior of the form and use AJAX to save the form.

Let say form is your form. You would do the following:

form.submit(function (e) {
    e.preventDefault();
    // ...and then your AJAX call.
    // I won't go into the mechanics of AJAX,
    // as there is lots of documentation online.
    // Since you're using jQuery, I recommend http://api.jquery.com/jQuery.ajax/.
    // Then, in your success function I would use your code:
    $.ajax({
        success: function () {
            $("#submit").val("Update");
        }
    });
});
$("#submit").click(function(){
$("#submit").val("Update");
});

In this code the #Submit should be unique in the DOM. There shouldn't be any other element in the DOM with id submit. If there exists such an element the code will show irregularity. I suspect that there may be more than one id=submit. try below code.

<input type="submit" id="submitButtonOne" value="Save" />

$("#submitButtonOne").click(function(){
$("#submitButtonOne").val("Update");
});

EDIT:

If your button is rendering dynamically try this

$(document).ready(function(){

    $('body').on('click','#submitButtonOne',function(){

        $(this).val("Update");

    })


});

make sure that you have loaded jquery library.

try this

  $(function(){
   $("#submit").click(function(){

        $(this).val("Update");
       return false;
    });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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