单击或更改输入,禁用其他人

时间:2015-09-21 18:42:02

标签: jquery events input disabled-input



$(function() {
    $('.disablesothers').on('change focus click', function () {
	  var $input = $(this).find('input');
	  var $brothers = $input.parent().siblings('.disablesothers');
	  console.log($brothers.length);
      $input.removeAttr('disabled');
	  $brothers.find('input').attr('disabled', 'disabled');
    });
});

input:disabled {
   border: 1px solid red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="overlabel tab_content default tooltip disablesothers" title="Bind text">
  <span>Destination name, zone</span>
  <input type="text" name="hotel_name">
</label>
<label class="overlabel disablesothers">
  <input type="text" name="other_hotels" id="other_hotels">
</label>
&#13;
&#13;
&#13;

请注意,我在标签上绑定事件,而不是输入,

但这不起作用

知道为什么吗?

- 编辑 -

这可能是因为removeAttr它实际上没有删除它,只是清除了值。?

2 个答案:

答案 0 :(得分:0)

您需要使用.prop('disabled', true)代替.attr('disabled', 'disabled')

$(function() {
    $('.disablesothers').on('change focus click', function () {
	  var $input = $(this).find('input');
	  var $brothers = $input.parent().siblings('.disablesothers');
	  console.log($brothers.length);
      $input.removeAttr('disabled');
	  $brothers.find('input').prop('disabled', true);
    });
});
input:disabled {
   border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="overlabel tab_content default tooltip disablesothers" title="Bind text">
  <span>Destination name, zone</span>
  <input type="text" name="hotel_name">
</label>
<label class="overlabel disablesothers">
  <input type="text" name="other_hotels" id="other_hotels">
</label>

作为.attr()州的文档:

  

要检索和更改DOM属性,例如已选中,已选中,   或禁用表单元素的状态,请使用.prop()方法。

答案 1 :(得分:0)

我通过在禁用输入时添加一个覆盖标签的透明div来解决,因为如果某个浏览器是禁用的输入,则某些浏览器不会触发该事件,

<label class="overlabel disablesothers">
  <input type="text" name="other_hotels" id="other_hotels">
</label>

然后,

:checked + div {
    display: inline-block;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
}