表单元素的刷新和自动初始化

本章我们将研究刷新表单元素和防止表单元素自动初始化。


刷新表单元素

Refresh方法用于自行更新表单控件的新状态,用JavaScript更新表单控件。 以下代码片段用于不同的表单元素 −

复选框

$( "input[type = 'checkbox']" ).prop( "checked", true ).checkboxradio( "refresh" );

Radios

$( "input[type = 'radio']" ).prop( "checked", true ).checkboxradio( "refresh" );

选择框

var myselect  =  $( "#selectfoo" );
myselect[0].selectedIndex = 3;
myselect.selectmenu( "refresh" );

滑块

$( "input[type = 'range']" ).val( 60 ).slider( "refresh" );

翻转开关

var myswitch = $( "#selectbar" );
myswitch[ 0 ].selectedIndex = 1;
myswitch.slider( "refresh" );

防止表单元素自动初始化

包含属性 data-role = "none" 以修改用于防止自动初始化的选择器。将 data-role = "none" 属性与 mobileinit 事件绑定以加载第一页和后续页面 .

<label for = "test">
<select id = "test" name = "test" data-role = "none">
   <option value = "Mumbai">Mumbai</option>
   <option value = "Pune">Pune</option>
   <option value = "Belgaum">Belgaum</option>
   <option value = "Chennai">Chennai</option>
   <option value = "Bangalore">Bangalore</option>
</select>
$( document ).bind( "mobileinit", function() {
   $.mobile.page.prototype.options.keepNative = "select, input.test, textarea.bar";
});

❮ jQuery Mobile - 表单