Jquery 使用if-else语句检查输入时修改属性

Jquery 使用if-else语句检查输入时修改属性,jquery,Jquery,我有一组单选按钮、一个文本字段、一个复选框和一个隐藏的文本字段 <input type="radio" name="amount-value" value="33"> $ 33 <br> <input type="radio" name="amount-value" value="50"> $ 50 <br> <input type="radio" name="amount-value" value="100"> $

我有一组单选按钮、一个文本字段、一个复选框和一个隐藏的文本字段

<input type="radio" name="amount-value" value="33">   $ 33   <br>
<input type="radio" name="amount-value" value="50">   $ 50   <br>
<input type="radio" name="amount-value" value="100">  $ 100  <br>
<input type="radio" name="amount-value" value="250">  $ 250  <br>
<input type="radio" name="amount-value" value="500">  $ 500  <br>
<input type="radio" name="amount-value" value="1000"> $ 1000 <br>
<input type="radio" name="amount-value" value="3333"> $ 3,333<br>
<input type="radio" name="amount-value" value="5000"> $ 5,000<br>
<input type="radio" name="amount-value" value="0" id="other-amount">                
  $ <input type="text" name="" id="other-amount-val" disabled="disabled">

<p><input type="checkbox" id="rainforest_guardian">I'd like to become a Rainforest Guardian by making this an ongoing monthly gift.</p>         

<input id="donation_amount" name="amount" value="" type="hidden" >
33美元
50美元
100美元
250美元
500美元
1000美元
3333美元
5000美元
$ 我想成为一名雨林卫士,将此作为一份持续的月度礼物。

选择最后一个单选按钮“其他金额”时,我想启用“其他金额val”文本字段

如果选中“雨林卫士”复选框,我想将“捐赠金额”的名称属性更改为“a3”

这是我尝试使用的JS,但无法确定我做错了什么。Web Inspector在if语句中给了我“未定义的不是函数”

<script>

$(document).ready(function() {


    // Enable Amount input box if radio button is selected

    $('#other-amount').change(function() {
      if (('#other-amount').prop('checked', true)) {
        $('#other-amount-val').prop('disabled', false);
      } else {
        $('#other-amount-val').prop('disabled', true );
      }
    });

    // Change name attribute value if checkbox is selected

   $('#rainforest_guardian').click(function() {
      if (('#rainforest_guardian').is(':checked')) {
        $('#donation_amount').prop('name', "a3");
      } else {
        $('#donation_amount').prop('name', "amount" );
      }
    });

</script>

$(文档).ready(函数(){
//如果选择单选按钮,则启用金额输入框
$(“#其他金额”)。更改(函数(){
如果(“#其他金额”).prop('checked',true)){
$(“#其他金额val”).prop('disabled',false);
}否则{
$(“#其他金额val”).prop('disabled',true);
}
});
//如果选中复选框,则更改名称属性值
$(“#雨林守护者”)。单击(函数(){
如果(“#雨林守护者”)是(“:选中”)){
美元(‘捐赠金额’).prop(‘名称’,‘a3’);
}否则{
美元(‘捐赠金额’).prop(‘名称’,‘金额’);
}
});
我在StackExchange上看过几篇相关文章,并尝试了不同的方法,例如单击()和更改(),如上所示


请告诉我我做错了什么。提前谢谢。

您在一些if语句中忘记了
$
,并且错过了结束标记
})

应该是

if ($('#other-amount').prop('checked', true)) {
    ^
$('#other-amount').prop('checked')
$('#rainforest_guardian').is(':checked')
试试这个:

$(document).ready(function () {
    // Enable Amount input box if radio button is selected
    $('#other-amount').change(function () {
        if ($('#other-amount').prop('checked', true)) {
            $('#other-amount-val').prop('disabled', false);
        } else {
            $('#other-amount-val').prop('disabled', true);
        }
    });
    // Change name attribute value if checkbox is selected
    $('#rainforest_guardian').click(function () {
        if ($('#rainforest_guardian').is(':checked')) {
            $('#donation_amount').prop('name', "a3");
        } else {
            $('#donation_amount').prop('name', "amount");
        }
    });
});
应该是

if ($('#other-amount').prop('checked', true)) {
    ^
$('#other-amount').prop('checked')
$('#rainforest_guardian').is(':checked')

应该是

if ($('#other-amount').prop('checked', true)) {
    ^
$('#other-amount').prop('checked')
$('#rainforest_guardian').is(':checked')

你的,准备好了还没完成。愚蠢的错误。谢谢Sergio和@Musa。错误已排序,但第二个函数没有更改输入的name属性。你能告诉我为什么吗?啊。它在小提琴上。只是不在我当地的环境中。谢谢你的帮助。