简体   繁体   中英

jQuery can change the text of input submit type but button is no longer clickable

I am using jaquery and jquery mobile version 1.8 and I have a button like so:

   <div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%">
        <input type="submit" name="Next" id="NextButton" value="Save"  /> 
   </div>

And I have a javascript that can change the text for it like so:

     $('#AnyButton').live('click', function() {             
        if(true)
        {
            $('#myButton div').text('Saving')
        }
        else
            $('#myButton div').text('Continue');
    });

I tried so many other ways that didn't work but this works however after I change the text the button seems to replace the myButton div content with the text Saving or Continue and thus the button is no longer clickable.

In my browser debugger the button shows a text Save appear between myButton and the Nextbutton input.

Like so:

<div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%">
"Save"
      <input type="submit" name="Next" id="NextButton" value="Save"  /> 
</div>

If I am reading your post correctly, you want to change the text of the input, you need to change it's value property. I think .live is relatively old and deprecated and should be replaced with .on and a delegated event handler

$('#AnyButton').live('click', function() {             
    if(true)
        $('#myButton').find('input[type="submit"]').val('Saving');
    else
        $('#myButton').find('input[type="submit"]').val('Continue');
});

I suspect there is more to your code than you have presented. With jQuery Mobile, each input is read as a Button. So you mnay need to refresh it after a dynamic update.

This code is working:

 $(function() { $("#NextButton").click(function(e) { e.preventDefault(); $(this).val("Saving").button("refresh"); }); });
 <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <div class="ui-bar-a" id="myButton" style="bottom:0;position: fixed;width: 100%"> <input type="submit" name="Next" id="NextButton" value="Save" /> </div>

See More: https://api.jquerymobile.com/button/ and https://demos.jquerymobile.com/1.4.5/button/

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