JQuery - 表单重置 - 排除“选择”框 [英] JQuery - Form Reset - Exclude "Select" box

查看:85
本文介绍了JQuery - 表单重置 - 排除“选择”框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我可以使用以下JQuery语法重置所有表单元素:

I can reset all my form elements using the following JQuery Syntax:

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

如何修改此项以排除重置选择框值?

How can I modify this to exclude the reset of "select box" values?

谢谢

推荐答案

给大家..

重置函数没有将所有内容设置为''(空字符串)

the reset function does not set everything to '' (empty string)

它重置为初始值..(存储在value属性中,或者选择的选项等..)

it reset to their initial values .. (stored in the value attribute, or selected option etc..)

如果你想保持默认的重置功能,那么你应该

If you want to maintain the default reset features then you should


  1. 获取所有< select> 元素

  2. 获取当前选择的值

  3. 按照您目前的方式重置表单

  4. 重新设置选定的

  1. get all the <select> elements
  2. get their currently selected values
  3. reset the form as you currently do
  4. re-set the selected

示例:

<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>

这篇关于JQuery - 表单重置 - 排除“选择”框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆