【问题标题】:clear form element values within specific div or span using jquery使用 jquery 清除特定 div 或 span 内的表单元素值
【发布时间】:2014-10-17 06:55:08
【问题描述】:

如何使用 jQuery 清除特定 div 中的表单元素值,我的表单中有超过 25 个字段,我需要清除所有类型的表单元素值的函数。我尝试了基本的 jQuery 函数

$('#div_id input[type="text"]').val('');

【问题讨论】:

  • $('#div_id *').val('');

标签: jquery html


【解决方案1】:

您好,您可以使用下面的代码来解决您的问题,我希望它能正常工作

function clear_form_elements(id_name) {
  jQuery("#"+id_name).find(':input').each(function() {
    switch(this.type) {
        case 'password':
        case 'text':
        case 'textarea':
        case 'file':
        case 'select-one':       
            jQuery(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
  });
}

【讨论】:

    【解决方案2】:

    对于所有类型的输入字段(包括选择和文本区域),请尝试:input-selector

    $('#div_id :input').val('');
    

    您的选择器将仅清除那些作为显式 type="text" 设置的 input 元素,这意味着 <input name="abc" /> 不会被清除

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      相关资源
      最近更新 更多