jQuery 在聚焦特定输入字段时禁用输入字段

我有两个输入字段,如果另一个输入字段中有值,我希望禁用另一个输入字段。我尝试使用 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>

查询


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


繁星淼淼
浏览 78回答 3
3回答

慕码人8056858

你可以这样做:$(document).ready(function() {&nbsp; $("#fixedSalary").on("input", function() {&nbsp; &nbsp; if ($("#fixedSalary").val() != "") {&nbsp; &nbsp; &nbsp; $("#selfEmployed").prop('disabled', true);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $("#selfEmployed").prop('disabled', false);&nbsp; &nbsp; }&nbsp; });&nbsp; $("#selfEmployed").on("input", function() {&nbsp; &nbsp; if ($("#selfEmployed").val() != "") {&nbsp; &nbsp; &nbsp; $("#fixedSalary").prop('disabled', true);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $("#fixedSalary").prop('disabled', false);&nbsp; &nbsp; }&nbsp; });});$("#btnReset").click(function() {&nbsp; $("#fixedSalary").prop('disabled', false);&nbsp; $("#selfEmployed").prop('disabled', false);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul>&nbsp; <li id="fixedSalaryList"><b>Fixed Salary (Monthly)</b>&nbsp; &nbsp; <a href="#" class="show-input">$</a>&nbsp; &nbsp; <input type="text" name="salary[]" id="fixedSalary" class="assest-input inputNumber" inputmode="numeric">&nbsp; </li>&nbsp; <li id="selfEmployedList"><b>Self-Employed Income (Annually)</b>&nbsp; &nbsp; <a href="#" class="show-input">$</a>&nbsp; &nbsp; <input type="text" name="selfEmployedIncome[]" id="selfEmployed" class="assest-input inputNumber" inputmode="numeric">&nbsp; </li></ul><input type="submit" text="Reset" id="btnReset">

慕丝7291255

change()你可以用这样的事件来做查询$(document).ready(function(){&nbsp; $("#selfEmployed").change(function(){&nbsp; &nbsp;$("#fixedSalary").prop('disabled', true);&nbsp; });&nbsp; $("#fixedSalary").change(function(){&nbsp; &nbsp;$("#selfEmployed").prop('disabled', true);&nbsp; });});现在它可以双向工作。

婷婷同学_

这正是您要找的$(document).ready(function(){&nbsp; $("#selfEmployed").keyup(function(){&nbsp; if(!$("#selfEmployed").val()){&nbsp; $("#fixedSalary").prop('disabled', false);&nbsp; }&nbsp;&nbsp; else {&nbsp; &nbsp;$("#fixedSalary").prop('disabled', true);&nbsp; &nbsp;}});&nbsp; $("#fixedSalary").keyup(function(){&nbsp; if(!$("#fixedSalary").val()) {&nbsp; $("#selfEmployed").prop('disabled', false);&nbsp; }&nbsp;&nbsp; else {&nbsp; &nbsp;$("#selfEmployed").prop('disabled', true);&nbsp; &nbsp;}&nbsp; });&nbsp;});注意:请记住,我们正在对keyup()事件执行此操作,因此即使您删除了输入,它也会起作用。
打开App,查看更多内容
随时随地看视频慕课网APP