亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當特定輸入字段聚焦時,jQuery禁用輸入字段

當特定輸入字段聚焦時,jQuery禁用輸入字段

UYOU 2022-06-16 10:52:29
我有兩個輸入字段,如果另一個有值,我希望禁用另一個。我使用 jQuery 進行了嘗試,但沒有任何反應。有人可以知道我該怎么做嗎?謝謝我的輸入框<ul><li id="fixedSalaryList"><b>Fixed Salary (Monthly)</b>    <a href="#" class="show-input">$<?php echo $data['app1']['salary']; ?> <img src="<?php echo $dirimg; ?>/arrow-right.svg"></a>    <input type="text" name="salary[]" id="fixedSalary" class="assest-input inputNumber" value="<?php echo $data['app1']['salary']; ?>" inputmode="numeric"></li><li id="selfEmployedList"><b>Self-Employed Income (Annually)</b>    <a href="#" class="show-input">$<?php echo $data['app1']['selfEmployedIncome']; ?><img src="<?php echo $dirimg; ?>/arrow-right.svg"></a>    <input type="text" name="selfEmployedIncome[]" id="selfEmployed" class="assest-input inputNumber" value="<?php echo $data['app1']['selfEmployedIncome']; ?>" inputmode="numeric"></li></ul>jQuery $('input').on('input', function() {    $(this).closest('li').find('input').not(this).prop('disabled', this.value.length)  });$('li input').on('input', function() {    $(this).closest('li').find('input').not(this).prop('disabled', this.value.length)});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul><li id="fixedSalaryList"><b>Fixed Salary (Monthly)</b>    <a href="#" class="show-input">$</a>    <input type="text" name="salary[]" id="fixedSalary" class="assest-input inputNumber"  inputmode="numeric"></li><li id="selfEmployedList"><b>Self-Employed Income (Annually)</b>    <a href="#" class="show-input">$</a>    <input type="text" name="selfEmployedIncome[]" id="selfEmployed" class="assest-input inputNumber"  inputmode="numeric"></li></ul>
查看完整描述

3 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

你可以這樣做:


$(document).ready(function() {

  $("#fixedSalary").on("input", function() {

    if ($("#fixedSalary").val() != "") {

      $("#selfEmployed").prop('disabled', true);

    } else {

      $("#selfEmployed").prop('disabled', false);

    }

  });


  $("#selfEmployed").on("input", function() {

    if ($("#selfEmployed").val() != "") {

      $("#fixedSalary").prop('disabled', true);

    } else {

      $("#fixedSalary").prop('disabled', false);

    }

  });

});


$("#btnReset").click(function() {

  $("#fixedSalary").prop('disabled', false);

  $("#selfEmployed").prop('disabled', false);

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul>

  <li id="fixedSalaryList"><b>Fixed Salary (Monthly)</b>

    <a href="#" class="show-input">$</a>

    <input type="text" name="salary[]" id="fixedSalary" class="assest-input inputNumber" inputmode="numeric">

  </li>

  <li id="selfEmployedList"><b>Self-Employed Income (Annually)</b>

    <a href="#" class="show-input">$</a>

    <input type="text" name="selfEmployedIncome[]" id="selfEmployed" class="assest-input inputNumber" inputmode="numeric">

  </li>

</ul>


<input type="submit" text="Reset" id="btnReset">


查看完整回答
反對 回復 2022-06-16
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

你可以通過這樣的change()活動來做到這一點


jQuery


$(document).ready(function(){

  $("#selfEmployed").change(function(){

   $("#fixedSalary").prop('disabled', true);

  });


  $("#fixedSalary").change(function(){

   $("#selfEmployed").prop('disabled', true);

  });


});

現在它將雙向工作。


查看完整回答
反對 回復 2022-06-16
?
DIEA

TA貢獻1820條經驗 獲得超3個贊

這正是您正在尋找的


$(document).ready(function(){

  $("#selfEmployed").keyup(function(){

  if(!$("#selfEmployed").val()){

  $("#fixedSalary").prop('disabled', false);

  } 

  else {

   $("#fixedSalary").prop('disabled', true);

   }

});



  $("#fixedSalary").keyup(function(){

  if(!$("#fixedSalary").val()) {

  $("#selfEmployed").prop('disabled', false);

  } 

  else {

   $("#selfEmployed").prop('disabled', true);

   }

  }); 

});


注意:請記住,我們是在keyup()活動中執行此操作的,因此即使您刪除輸入,它也會起作用。


查看完整回答
反對 回復 2022-06-16
  • 3 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號