如何禁用输入字段,但不禁用单击的输入字段,然后启用所有输入字段

How to disable input fields but not the clicked one and then enable all

本文关键字:字段 输入 然后 启用 单击 何禁用      更新时间:2023-09-26

我的项目中有一些按钮,如果单击一个按钮而不是单击的按钮,我想禁用所有按钮。当我再次单击该按钮时,我想启用所有这些按钮。这是我尝试过的,但没有奏效:

$('#TOSTORAGE').click(function(){
  $('#TOULD').prop('disabled', true'false);
  $('#TOTRAIN').prop('disabled', true'false);
  $('#TOARRIVAL').prop('disabled', true'false);
  $('#TOHANDLER').prop('disabled', true'false);
});
$('#TOULD').click(function(){
  $('#TOSTORAGE').prop('disabled', true'false);
  $('#TOTRAIN').prop('disabled', true'false);
  $('#TOARRIVAL').prop('disabled', true'false);
  $('#TOHANDLER').prop('disabled', true'false);
});
$('#TOTRAIN').click(function(){
  $('#TOSTORAGE').prop('disabled', true'false);
  $('#TOULD').prop('disabled', true'false);
  $('#TOARRIVAL').prop('disabled', true'false);
  $('#TOHANDLER').prop('disabled', true'false);
});
$('#TOARRIVAL').click(function(){
  $('#TOSTORAGE').prop('disabled', true'false);
  $('#TOULD').prop('disabled', true'false);
  $('#TOTRAIN').prop('disabled', true'false);
  $('#TOHANDLER').prop('disabled', true'false);
});
$('#TOHANDLER').click(function(){
  $('#TOSTORAGE').prop('disabled', true'false);
  $('#TOULD').prop('disabled', true'false);
  $('#TOTRAIN').prop('disabled', true'false);
  $('#TOARRIVAL').prop('disabled', true'false);
});
.btn-hl2{
color:#ffffff;
height:90px;
width: 340px;
    display: inline-block;
    margin: 6px;
    text-align: center;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    background-color: #536e7f;
    border: 1px solid #ffffff;
    white-space: nowrap;
    padding: 4px 10px;
    font-size: 34px;
    font-family: inherit;
    line-height: 1.42857143;
    border-radius: 4px;
    -webkit-user-select: none;
    font:#FFF;    
}
.btn-hl3{
color:#ffffff;
height:90px;
width: 695px;
    display: inline-block;
    margin: 6px;
    text-align: center;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    background-color: #536e7f;
    border: 1px solid #ffffff;
    white-space: nowrap;
    padding: 4px 10px;
    font-size: 34px;
    font-family: inherit;
    line-height: 1.42857143;
    border-radius: 4px;
    -webkit-user-select: none;
    font:#FFF;    
}
.btn-hl2:hover {
        background-color: #e4b527;
    }
.btn-hl3:hover {
        background-color: #e4b527;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="TOSTORAGE" type="submit" class="btn-hl2" name="TOSTORAGE">
  <span aria-hidden="true"></span> TO STORAGE
</button>  
<button id="TOULD" type="submit" class="btn-hl2" name="TOULD">
  <span aria-hidden="true"></span> TO ULD
</button>
<button id="TOTRAIN" type="submit" class="btn-hl2" name="TOTRAIN">
  <span aria-hidden="true"></span> TO TRAIN
</button>
<button id="TOARRIVAL" type="submit" class="btn-hl2" name="TOARRIVAL">
  <span aria-hidden="true"></span> TO ARRIVAL
</button>
<button id="TOHANDLER" type="submit" class="btn-hl3" name="TOHANDLER">
  <span aria-hidden="true"></span> TO HANDLER
</button>

您可以通过以下方式设置公共类名并实现:

$('.btn-common').click(function(){
	$('.btn-common').prop('disabled', true);
	$(this).prop('disabled', false);
});
.btn-hl2{
    color:#ffffff;
    height:90px;
    width: 340px;
    display: inline-block;
    margin: 6px;
    text-align: center;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    background-color: #536e7f;
    border: 1px solid #ffffff;
    white-space: nowrap;
    padding: 4px 10px;
    font-size: 34px;
    font-family: inherit;
    line-height: 1.42857143;
    border-radius: 4px;
    -webkit-user-select: none;
    font:#FFF;    
}
.btn-hl3{
    color:#ffffff;
    height:90px;
    width: 695px;
    display: inline-block;
    margin: 6px;
    text-align: center;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    background-color: #536e7f;
    border: 1px solid #ffffff;
    white-space: nowrap;
    padding: 4px 10px;
    font-size: 34px;
    font-family: inherit;
    line-height: 1.42857143;
    border-radius: 4px;
    -webkit-user-select: none;
    font:#FFF;    
}
.btn-common:disabled {
    background: #dddddd;
}
.btn-hl2:hover {
    background-color: #e4b527;
}
.btn-hl3:hover {
    background-color: #e4b527;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="TOSTORAGE" type="submit" class="btn-hl2 btn-common" name="TOSTORAGE">
  <span aria-hidden="true"></span> TO STORAGE
</button>  
<button id="TOULD" type="submit" class="btn-hl2 btn-common" name="TOULD">
  <span aria-hidden="true"></span> TO ULD
</button>
<button id="TOTRAIN" type="submit" class="btn-hl2 btn-common" name="TOTRAIN">
  <span aria-hidden="true"></span> TO TRAIN
</button>
<button id="TOARRIVAL" type="submit" class="btn-hl2 btn-common" name="TOARRIVAL">
  <span aria-hidden="true"></span> TO ARRIVAL
</button>
<button id="TOHANDLER" type="submit" class="btn-hl3 btn-common" name="TOHANDLER">
  <span aria-hidden="true"></span> TO HANDLER
</button>

试试这个:

$('button').click(function() {
  $('button').not(this).prop('disabled', function(i, state) {
    return !state;
  });
});
button:disabled{
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>button 1</button>
<button>button 2</button>
<button>button 3</button>
<button>button 4</button>

( edit1 使用更好的选择器 .not( ) 而不是 .siblings( ) )

( edit2 简化解决方案)

Check this demo using jquery

.HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="TOSTORAGE" type="submit" class="btn-hl2" name="TOSTORAGE">
  <span aria-hidden="true"></span> TO STORAGE
</button>  
<button id="TOULD" type="submit" class="btn-hl2" name="TOULD">
  <span aria-hidden="true"></span> TO ULD
</button>
<button id="TOTRAIN" type="submit" class="btn-hl2" name="TOTRAIN">
  <span aria-hidden="true"></span> TO TRAIN
</button>
<button id="TOARRIVAL" type="submit" class="btn-hl2" name="TOARRIVAL">
  <span aria-hidden="true"></span> TO ARRIVAL
</button>
<button id="TOHANDLER" type="submit" class="btn-hl3" name="TOHANDLER">
  <span aria-hidden="true"></span> TO HANDLER
</button>

.JS

(function($) {
    $.fn.enableDisable = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);
$('button').click(function(e) {
  $('button').not(this).enableDisable();
});

小提琴演示