Reset values from jQuery Mobile

2019-02-27 04:01发布

问题:

I need to reset all the values ​​of field elements of my page. The elements are: inputs, selects (combobox), checkbox and radio group. Searching found the following code:

$("*").attr('value', '');
$("input[type='checkbox']").attr("checked",false); 

$('select').each(function() {
    if($(this).children().length > 0) {
        $($(this).children()[0]).attr('selected', 'selected');
        $(this).change();
    }
});

Inputs and checkbox are ok with this code, but the other components present problems with the codes tested. The radio group needed or take any kind of selection or at least select the first. The combobox resets with this code but when trying to select a new value it is not storing your value.

Thanks!

回答1:

The non textual inputs need to be refreshed. Here's an example of doing so:

//reset text input values, then refresh slider widgets
$(".ui-input-text").val('').filter('.ui-slider-input').slider('refresh');

//reset checkboxes and radio inputs, then refresh them
$(".ui-checkbox input[type='checkbox'], .ui-radio input[type='radio']").prop("checked", false).checkboxradio("refresh"); 

//reset all select menus and then refresh them
$('.ui-select select').val('').selectmenu('refresh');
return false;

Here is a demo: http://jsfiddle.net/MLewd/1/