Javascript 当另一个文本框的值为HTML时禁用该文本框

Javascript 当另一个文本框的值为HTML时禁用该文本框,javascript,jquery,Javascript,Jquery,对于这个问题我很抱歉,但是如果另一个文本框有一个值,我如何禁用该文本框?? 我已经尝试过这段代码,但是它不起作用,很抱歉我没有回答这个问题 function disable(downpayment,full_payment) { if ( downpayment.value.length >= 1 ) document.getElementById(full_payment).disabled = true; else document.getElementById(full_pay

对于这个问题我很抱歉,但是如果另一个文本框有一个值,我如何禁用该文本框?? 我已经尝试过这段代码,但是它不起作用,很抱歉我没有回答这个问题

function disable(downpayment,full_payment)
{

if ( downpayment.value.length >= 1 )
document.getElementById(full_payment).disabled = true;

else

document.getElementById(full_payment).disabled = false;
}



</script>
        <input name="downpayment" id="downpayment" type="text" onselect="function disable(downpayment,full_payment);" style="width:250px" />
      </p>
      <p>
        <input name="full_payment" id="full_payment" type="text" style="width:250px" />
功能禁用(首付、全额支付)
{
如果(首付款.value.length>=1)
document.getElementById(全额付款).disabled=true;
其他的
document.getElementById(全额付款).disabled=false;
}


您已经将问题标记为jQuery,因此它非常简单

$("#downpayment").on("change paste", function() { 
                         $("#full_payment").prop("disabled", this.value.length); 
                          });

一旦您的首付款中包含一些内容(即使是一个空格,如果这不理想,
$.trim()
在您检查其
长度
属性之前输入内容),它将启用全额付款。

如果您想继续使用纯JavaScript:

// finding the relevant elements *outside* the function
var downpayment = document.getElementById('downpayment'),
    full_payment = document.getElementById('full_payment');

function enableToggle(current, other) {
    /* 'current' is the element that currently has focus
       'other' is the other input element, that does not have focus.

    1. if the 'current' value of the focused/active element, once the whitespace
       is removed, is greater than 0 (so it has something in it other than whitespace,
       the disabled property of the 'other' element is true,
    2. if the 'current' element has only whitespace, and/or a zero-length value,
       the 'other' element's disabled property is false.
    */

    other.disabled = current.value.replace(/\s+/,'').length > 0;
}

// using the onkeyup event to call a function on the elements.
downpayment.onkeyup = function () {
    enableToggle(this, full_payment);
}
full_payment.onkeyup = function () {
    enableToggle(this, downpayment);
}
这适用于以下HTML:

<input name="downpayment" id="downpayment" type="text" style="width:250px" />
<input name="full_payment" id="full_payment" type="text" style="width:250px" />
(这与上面的JavaScript完全相同,注释被去掉,但被包装在
标记中)

将其放在HTML的底部意味着在您尝试将事件处理程序分配给元素之前,这些元素存在于DOM中

顺便说一句,使用调整后的HTML,给出:

<form>
<!--
     I associated the relevant elements with a class-name 'enableToggle',
     you don't have to, it just reduces the work the jQuery has to do later
     when using siblings('.enableToggle') to find the relevant elements.
-->
<div>
    <label for="downpayment">Downpayment</label>
    <input name="downpayment" class="enableToggle" id="downpayment" type="text" style="width:250px" />
    <label for="full_payment">Full payment</label>
    <input name="full_payment" class="enableToggle" id="full_payment" type="text" style="width:250px" />
</div>
</form>

.

我对如何使用它感到困惑,你能输入真实完整的脚本吗,,:(好东西,但我们真的需要三元组吗?谢谢,这是我能看到的最简单的方法,没有足够的检查或足够的可能状态来证明使用
if
/
else
块的合理性。你看到更简单的方法了吗?
other.disabled=current.value.replace(/\s+/,“”).length>0
应该足够了。@alex:谢谢!我……真不敢相信我错过了结束评论。我已经更新了
返回值
,也转储了三元值;谢谢!我真不敢相信我忽略了这一点..!XD
<form>
<!--
     I associated the relevant elements with a class-name 'enableToggle',
     you don't have to, it just reduces the work the jQuery has to do later
     when using siblings('.enableToggle') to find the relevant elements.
-->
<div>
    <label for="downpayment">Downpayment</label>
    <input name="downpayment" class="enableToggle" id="downpayment" type="text" style="width:250px" />
    <label for="full_payment">Full payment</label>
    <input name="full_payment" class="enableToggle" id="full_payment" type="text" style="width:250px" />
</div>
</form>
// binds both event-handler to both 'keyup' and 'paste' events.
$('.enableToggle').on('keyup paste', function(){
    /* 'curVal' is a Boolean (true or false) depending on whether there's
        a value other than whitespace */
    var curVal = $(this).val().replace(/\s+/g,'').length > 0;

    /* sets the 'disabled' property of the sibling elements of the current
       element, as long as those siblings have the class 'enableToggle'
       (this avoids having to explicitly identify all the elements you want
       to act on). */
    $(this).siblings('.enableToggle').prop('disabled', curVal);
});
$(document).ready(function()
{   
    $(".PaxHeads").on('keypress','input[name="DebitAmount"]',function()
    {
         var myLength = $('input[name="DebitAmount"]').val().length;
         if (myLength!=0)
         {
            $('input[name="CreditAmount"]').attr("disabled", "disabled");   
         }
        else 
        {
            $('input[name="CreditAmount"]').removeAttr("disabled"); 
        }
    });
    $(".PaxHeads").on('keypress', 'input[name="CreditAmount"]', function()
    {
        var myLength1 = $('input[name="CreditAmount"]').val().length;
        if (meLength1!=0)
        {
            $('input[name="DebitAmount"]').attr("disabled", "disabled");    
        }
        else
        {
            $('input[name="DebitAmount"]').removeAttr("disabled");      
        }
    });
});