简体   繁体   English

按钮清除所有选择字段

[英]button to clear all select fields

 <form method="post" action="asdasd" class="custom" id="search">
 <select name="sel1" id="sel1">
   <option value="all">all</option>
   <option value="val1">val1</option>
   <option value="val2" selected="selected">val2</option>
 </select>
 <select name="sel2" id="sel2">
   <option value="all">all</option>
   <option value="val3">val3</option>
   <option value="val4" selected="selected">val4</option>
 </select>
 </form>

I want to select the first field of all the Select (so set the value "all" to all the Select) clicking a button 我想选择所有选择的第一个字段(因此将值“ all”设置为所有选择)单击一个按钮

Your form should have a reset button 您的表格应该有一个重置按钮

<form method="post" action="asdasd" class="custom" id="search">
 <select name="sel1" id="sel1">
   <option value="all">all</option>
   <option value="val1">val1</option>
   <option value="val2" selected="selected">val2</option>
 </select>
 <select name="sel2" id="sel2">
   <option value="all">all</option>
   <option value="val3">val3</option>
   <option value="val4" selected="selected">val4</option>
 </select>
 <input type="button" name="Reset" />
 </form>

After adding a reset button input element to your form below code should reset all select fields. 在下面的代码中向表单中添加一个重置按钮输入元素后,应重置所有选择字段。

jQuery( "input[name=Reset]" ).click( function(){ //jQuery click event
jQuery( "select" ).each(function() {     //for each select type
    $(this).val( "all" );
});

});

You have to add attribute selected to the first option of all select box. 您必须将selected的属性添加到所有选择框的第一个选项中。

$("select option:first").attr('selected','selected');

See my fiddle 我的小提琴

Hope it helps. 希望能帮助到你。

You don't need to use jquery. 您不需要使用jquery。 This can be done by JavaScript 这可以通过JavaScript完成

  document.getElementById("sel1").selectedIndex = 0;

Here is my JSFiddler demo for complete code. 这是我的JSFiddler演示,以获取完整的代码。

This is jQuery way to achieve above 这是上面实现的jQuery方法

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

Or to be more specific 或者更具体

  $("#sel1 , #sel2").val("0");

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

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