当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - clicking or changing input, disables others

问题描述:

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

网友答案:

You need to use .prop('disabled', true) instead of .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>
网友答案:

I solved by adding a transparent div wich covers the label when the input is disabled, because some browsers won't fire the event if it's a disabled input,

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

then,

:checked + div {
    display: inline-block;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: