1
votes

After applying the http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css and http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js

the text of the button cannot be changed by scripting $(this).val('new value');

Do anyone have similar experience and have the solution?

DEMO can be tried from FIDDLE

JQUERY:

$(document).ready(function () {
    $('#submit_btn').click(function (e) {
        e.preventDefault();
        $(this).val('Processing ...');
        $.ajax({
            cache: false,
            type: "POST",
            dataType: "json",
            data: $('#form1').serialize(),
            url: "echo/json",
            complete: function (HttpRequest, textStatus) {
                $(this).val('Create');
            }
        });
        return false;
    });
});

HTML:

<form action="call.php" method="POST" id="form1" name="form1">
    <input type="text" name="campname" id="campname">
    <textarea id="longdesc" name="longdesc"></textarea>
    <input type="text" name="vercode" id="vercode" />
    <input type="submit" value="Create" id="submit_btn" />
</form>
4
work if not applying query.mobile-1.3.2.min.css - Kelvin
dont use .ready() in jQM, instead use jQM events. - Omar

4 Answers

3
votes

Can you try this,

 $(".ui-btn-text").text('Processing ...');

Code:

$(document).ready(function () {
  $('#submit_btn').click(function (e) {        
    e.preventDefault();
    $(".ui-btn-text").text('Processing ...');
    $.ajax({
        cache: false,
        type: "POST",
        dataType: "json",
        data: $('#form1').serialize(),
        url: "echo/json",
        complete: function (HttpRequest, textStatus) {
           $(".ui-btn-text").text('Create');
        }
    });
    return false;
   });
});

Demo : http://jsfiddle.net/Rz2sJ/4/

2
votes

By the time the AJAX callback runs, this as a keyword has lost its context. You need to assign it to a variable to retain the reference. Something like this:

$(document).ready(function () {
    $('#submit_btn').click(function (e) {
        e.preventDefault();
        var button = $(this);
        button.val('Processing ...');
        $.ajax({
            cache: false,
            type: "POST",
            dataType: "json",
            data: $('#form1').serialize(),
            url: "echo/json",
            complete: function (HttpRequest, textStatus) {
                button.val('Create');
            }
        });
        return false;
    });
});
2
votes

You're working with .button() widget of jQuery Mobile. <button> is converted into a div to give it a new look by jQuery Mobile.

When doing any changes to <button> or <a>, you need to refresh that element to re-apply styles.

All you need to do is:

$(".selector").val('Processing ...').button("refresh");

Demo

0
votes

Final applying $('#button').prev().text('your new text'); work.

Just don't understanding why use .prev() before changing the text.