简体   繁体   English

以图形方式重置选择框

[英]reset a select box graphically

Using jquery mobile, I am trying to reset a form 使用jQuery Mobile,我正在尝试重置表单

I am using the following: 我正在使用以下内容:

$('form').prop('selectedIndex',0);

The value the select submits does reset to zero, but the image stays as it is. 选择提交的值确实重置为零,但图像保持原样。

Create vs. refresh: An important distinction 创建与刷新:重要区别
Note that there is an important difference between the create event and refresh method that some widgets have. 请注意,某些小部件具有的create event和refresh方法之间存在重要区别。 The create event is suited for enhancing raw markup that contains one or more widgets. create事件适用于增强包含一个或多个窗口小部件的原始标记。 The refresh method should be used on existing (already enhanced) widgets that have been manipulated programmatically and need the UI be updated to match. 刷新方法应用于已通过编程方式操作且需要更新UI才能匹配的现有(已经增强)的小部件。

For example, if you had a page where you dynamically appended a new unordered list with data-role=listview attribute after page creation, triggering create on a parent element of that list would transform it into a listview styled widget. 例如,如果您有一个页面,在页面创建后动态添加了一个新的具有data-role=listview属性的无序列表,则在该列表的父元素上触发create会将其转换为listview样式的小部件。 If more list items were then programmatically added, calling the listview's refresh method would update just those new list items to the enhanced state and leave the existing list items untouched. 如果随后以编程方式添加了更多列表项,则调用listview的refresh方法将仅将那些新列表项更新为增强状态,并使现有列表项保持不变。

Refreshing form elements 刷新表单元素
In jQuery Mobile, some enhanced form controls are simply styled (inputs), but others are custom controls (selects, sliders) built from, and kept in sync with, the native control. 在jQuery Mobile中,一些增强的表单控件只是简单地设置样式(输入),而另一些则是自定义控件(选择,滑块),它们是从本机控件构建的并与本机控件保持同步。 To programmatically update a form control with JavaScript, first manipulate the native control, then use the refresh method to tell the enhanced control to update itself to match the new state. 要使用JavaScript以编程方式更新表单控件,请先操作本机控件,然后使用refresh方法告诉增强型控件自己更新以匹配新状态。 Here are some examples of how to update common form controls, then call the refresh method: 以下是一些有关如何更新常见表单控件,然后调用refresh方法的示例:

Checkboxes: 复选框:

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

Radios: 收音机:

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

Selects: 选择:

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

Sliders: 滑杆:

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

Flip switches (they use slider): 拨动开关(它们使用滑条):

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

try: 尝试:

$('form').prop('selectedIndex',0);
$('form').trigger('create');

Or if this is a drop down select 或者,如果这是一个下拉列表,请选择

JS JS

$('form').prop('selectedIndex',0);
//refresh value         

$('select').selectmenu('refresh');

//refresh and force rebuild 
$('select').selectmenu('refresh', true);

假设您在select上有一个change事件...以编程方式更改索引后,调用change事件。

Assuming a select box like: 假设一个选择框像:

<select name="opt1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>

You can select the option you want like: 您可以选择所需的选项:

$("select").val("3");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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