如何使用jQuery从输入中删除选中的属性

时间:2018-07-19 03:51:03

标签: javascript jquery

我正在尝试从默认为选中状态的输入中删除选中属性。我正在使用同位素过滤器。

我尝试了

this.prop('checked', false); 

this.attr('checked', 'checked');

什么都不起作用...

输入

<input type="checkbox" name="files[]" value="" class="item 35 40" style="visibility:hidden;" checked>

javascript

proto.onHideTransitionEnd = function() {

  if ( this.isHidden ) {
    this.css({ display: 'none' });
    this.emitEvent('hide');
  }
};

4 个答案:

答案 0 :(得分:1)

这应该做到。

 this.removeAttr('checked');

答案 1 :(得分:0)

在 jQuery 1.6 +

要更改复选框的属性,应使用.prop()函数。

$('.prettycheckbox input').prop('checked', false);
$('.prettycheckbox input').prop('checked', true);

jQuery 1.5及更低版本

.prop()函数不存在,但是.attr()具有类似功能:

$('.prettycheckbox input').attr('checked', 'checked');

注意:removeAttr有效,但addAttr无效。

答案 2 :(得分:0)

没什么,这是完整的代码:

// remove element from DOM
proto.removeElem = function() {
  this.element.parentNode.removeChild( this.element );
  // remove display: none
  this.css({ display: '' });
  this.emitEvent( 'remove', [ this ] ); 


};



proto.remove = function() {
  // just remove element if no transition support or no transition
  if ( !transitionProperty || !parseFloat( this.layout.options.transitionDuration ) ) {
    this.removeElem();
    return;
  }

  // start transition
  this.once( 'transitionEnd', function() {
    this.removeElem();
  });
  this.hide();
};

proto.reveal = function() {
  delete this.isHidden;
  // remove display: none
  this.css({ display: '' }); 

  var options = this.layout.options;

  var onTransitionEnd = {};
  var transitionEndProperty = this.getHideRevealTransitionEndProperty('visibleStyle');
  onTransitionEnd[ transitionEndProperty ] = this.onRevealTransitionEnd;

  this.transition({
    from: options.hiddenStyle,
    to: options.visibleStyle,
    isCleaning: true,
    onTransitionEnd: onTransitionEnd
  });
};

proto.onRevealTransitionEnd = function() {
  // check if still visible
  // during transition, item may have been hidden
  if ( !this.isHidden ) {
    this.emitEvent('reveal');
  }
};

/**
 * get style property use for hide/reveal transition end
 * @param {String} styleProperty - hiddenStyle/visibleStyle
 * @returns {String}
 */
proto.getHideRevealTransitionEndProperty = function( styleProperty ) {
  var optionStyle = this.layout.options[ styleProperty ];
  // use opacity
  if ( optionStyle.opacity ) {
    return 'opacity';
  }
  // get first property
  for ( var prop in optionStyle ) {
    return prop;
  }
};

proto.hide = function() {
  // set flag
  this.isHidden = true;
  // remove display: none
  this.css({ display: '' });  

  var options = this.layout.options;

  var onTransitionEnd = {};
  var transitionEndProperty = this.getHideRevealTransitionEndProperty('hiddenStyle');
  onTransitionEnd[ transitionEndProperty ] = this.onHideTransitionEnd;

  this.transition({
    from: options.visibleStyle,
    to: options.hiddenStyle,
    // keep hidden stuff hidden
    isCleaning: true,
    onTransitionEnd: onTransitionEnd
  });
};

proto.onHideTransitionEnd = function() {
  // check if still hidden
  // during transition, item may have been un-hidden
  if ( this.isHidden ) {
    this.css({ display: 'none' }); 
    this.emitEvent('hide');

  }

};

答案 3 :(得分:-1)

这将解决以下问题:

$(document).ready(function(){
  if($('input[type="checkbox"]').is(':checked') == true){
      $('input[type="checkbox"]').prop('checked', false);
  }
});