jQuery - 使用.change事件清除表单字段

时间:2011-08-10 16:13:24

标签: javascript jquery

我使用以下jQuery代码根据与3个单选按钮相关联的更改事件切换div显示:

(function ($) {
  Drupal.behaviors.change_form_fields = {
    attach: function(context, settings) {
      $('div.form-item-payment-method input').each(function() {
        if (this.value != 'existing-bill') {
          $('div#' + this.value).css('display', 'none');
        }
      });

      $('div.form-item-payment-method input').change(function() {
        show_class = this.value;
        $('div.form-item-payment-method input').each(function() {
          if (this.value != show_class) {
            $('input#edit-' + this.value).val('');
            $('div#' + this.value).css('display', 'none');
          } else {
            $('div#' + this.value).css('display', 'block');
          }
        });
      });
    }
  }
}(jQuery));

这很好用,但我还想补充一些。

如果用户选择单选按钮,我想清空/清除其他组中的表单字段。就像onload(.each)一样,它们在显示内容周围有div。

我尝试使用.attr和.val,但是在选择另一个单选按钮时,表单字段不会清除。我错过了什么? jQuery 1.4x +

2 个答案:

答案 0 :(得分:1)

val('')应该可以解决问题,因此选择器或代码运行时必然存在其他问题。

答案 1 :(得分:0)

使用可以帮助您的单选按钮更改事件。

$("input[type=radio][name=groupname]").change(function(){

     //Code the clear the form fields or other logic here.
});