简体   繁体   English

使用Jquery重置选择框

[英]Using Jquery to reset a select box

I'm having difficulty with using jQuery to reset a value of a select box. 我在使用jQuery重置选择框的值时遇到了困难。 My code is as follows: 我的代码如下:

HTML: HTML:

<form id="myform">
  <select name="myselect" id="myselect" value=""> 
    <option value="">Default</option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
  </select>  
  <input type="submit" value="Go">
  <input type="reset" class="reset" value="Reset form">  
</form>

Javascript: 使用Javascript:

​$("#myform .reset").click(function() {
    $('#myselect').attr('value','');
    alert($('#myselect').attr('value'));
});​

As you can see option 2 is selected, but I want to reset the select box to "Default". 如您所见,选择了选项2,但我想将选择框重置为“默认”。 I've tried various methods including attr->selectedIndex but all have the same affect, it briefly changes to Default (As can be seen if you look at the form when the "Alert box" pops up, but as soon as the alert box is closed, or the alert line removed, it jumps back to the currently selected option 2. 我尝试了各种各样的方法,包括attr-> selectedIndex,但都有相同的效果,它会暂时更改为Default(如果你弹出“Alert box”时看到表单可以看到,但是一旦警告框出现关闭,或删除警报线,它会跳回当前选定的选项2。

I think this may be a jQuery bug, I've tried many different versions, from 1.6 to edge, all with same effect. 我认为这可能是一个jQuery错误,我已经尝试了许多不同的版本,从1.6到边缘,都具有相同的效果。

For ease I've added a jsfiddle: http://jsfiddle.net/ux2f2/1/ 为了方便起见,我添加了一个jsfiddle: http//jsfiddle.net/ux2f2/1/

Hope I've included everything as this is my first post, but im a long time reader :) Love this site! 希望我已经包含了所有内容,因为这是我的第一篇文章,但我很长一段时间读者:)喜欢这个网站!

You don't need any javascript to reset this simple form. 您不需要任何JavaScript来重置这个简单的表单。 Just use the attribute selected in your (X)HTML 只需使用(X)HTML中选择的属性即可

<form id="myform">
  <select name="myselect" id="myselect" value=""> 
    <option selected="selected">Default</option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
  </select>  
  <input type="submit" value="Go">
  <input type="reset" class="reset" value="Reset form">  
</form>

If you really want to use jQuery, we can do this : 如果你真的想使用jQuery,我们可以这样做:

$("#myform .reset").click(function() {
    $('#myselect').prop('selectedIndex', -1);
});

the value "-1", means "reset" 值“-1”表示“重置”

As Kolink has explained, the input of type "reset" is the cause of the problem as it resets the whole form to their default values. 正如Kolink所解释的那样,“重置”类型的输入是问题的原因,因为它将整个表单重置为其默认值。

The quickest solution would be to change 最快的解决方案是改变

<input type="reset" class="reset" value="Reset form">

to

<input type="button" class="reset" value="Reset form">

or just make a clickable anchor or div be the trigger for the reset instead of the input 或者只是使可点击的锚点或div成为重置的触发器而不是输入

This way you need not make any changes to your jQuery 这样您就不需要对jQuery进行任何更改

The main problem is that the reset button is resetting the form after you click it and see the alert. 主要问题是重置按钮在您单击它并查看警报后重置该表单。 You need to cancel the default event. 您需要取消默认事件。 However, you can also try vanilla JS: 但是,你也可以尝试vanilla JS:

// add id="reset" to the reset button's HTML, then...
document.getElementById('reset').onclick = function() {
    mysel = document.getElementById('myselect');
    mysel.selectedIndex = 0;
    alert(mysel.value);
    return false;
}

Updated Fiddle 更新小提琴

You should use something like: 你应该使用类似的东西:

$("select#myselect").val('').attr('selected', true);

Working fiddle of the above: http://jsfiddle.net/gsN9d/ 以上工作小提琴: http//jsfiddle.net/gsN9d/

As an alternate (newer syntax of "prop") use: 作为替代(“prop”的新语法)使用:

$("select#myselect").val('').prop('selected', true);

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

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