简体   繁体   English

JQuery - 表单重置 - 排除“选择”框

[英]JQuery - Form Reset - Exclude “Select” box

All, 所有,

I can reset all my form elements using the following JQuery Syntax: 我可以使用以下JQuery语法重置所有表单元素:

('#myform')[0].reset();

How can I modify this to exclude the reset of "select box" values? 如何修改此选项以排除重置“选择框”值?

Thanks 谢谢

To everyone.. 给大家..

the reset function does not set everything to '' (empty string) 重置功能不会将所有内容设置为''(空字符串)

it reset to their initial values .. (stored in the value attribute, or selected option etc..) 它重置为初始值..(存储在value属性中,或选择选项等)。

If you want to maintain the default reset features then you should 如果您想保持默认的重置功能,那么您应该这样做

  1. get all the <select> elements 获取所有<select>元素
  2. get their currently selected values 获取他们当前选择的值
  3. reset the form as you currently do 像你现在一样重置表单
  4. re-set the selected 重新设置所选

example: 例:

<script type="text/javascript">
  $(document).ready(
  function(){
   $("#resetbutton").click(
    function(){
     var values = [];
     var selected = $("select").each(
      function(){
       values.push( $(this).val());
       });
     this.form.reset();
     for (i=0;i<selected.length;i++)
      $(selected[i]).val(values[i]);
    });
    }
  );
 </script>

That's not jQuery, it's native javascript. 这不是jQuery,它是原生的javascript。 [0] brings out the actual DOM element, so it's the same as: [0]显示实际的DOM元素,因此它与:

document.getElementById('myform').reset();

reset() is a built-in browser implementation that resets the entire form. reset()是一个内置的浏览器实现,可以重置整个表单。 If you need to reset individual form types, try something like: 如果您需要重置单个表单类型,请尝试以下操作:

$('#myform :text').val('');

You can see all form selectors here: http://docs.jquery.com/Selectors 您可以在此处查看所有表单选择器: http//docs.jquery.com/Selectors

For whatever reason, David's right-on answer error'd my Google Chrome js. 无论出于何种原因,David的正确回答错误是我的Google Chrome js。 It's saying: 它说:

Uncaught TypeError: Property 'reset' of object # is not a function 未捕获的TypeError:对象#的属性“重置”不是函数

...so I decided to try a slightly different solution: ...所以我决定尝试一个稍微不同的解决方案:

  • Give native browser reset buttons attributes "hidden" and set "id": 将本机浏览器重置按钮属性设置为“隐藏”并设置“id”:

<input id="resetbutton" type="reset" style="visibility:hidden;" name="reset" value="reset" />

  • Initiate JQuery "click" event on reset button from clicking link: 通过单击链接在重置按钮上启动JQuery“单击”事件:

<a href="#" onclick="$('#resetbutton').click();">Reset</a>

You can do a fake reset by setting the values to an empty string and resetting the checkboxes using David's answer (or this more complete one ). 您可以通过将值设置为空字符串并使用David的答案(或更完整的答案)重置复选框来进行虚假重置。 You could also try storing each select element's value before resetting the form and then restore the values after: 您还可以在重置表单之前尝试存储每个select元素的值,然后在以下值之后恢复值:

var myform = $('#myform');
var selects = $('select', myform);

// Store each current value
selects.each(function() {
    $(this).data('previous', $(this).val()); 
});

myform[0].reset();

// Restore the values
selects.each(function() {
    $(this).val($(this).data('previous')); 
});

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

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