0

I have a set of radio buttons, a text field, a checkbox, and a hidden text field.

<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" >

When the last radio button, #other-amount, is selected, I want to enable #other-amount-val text field.

If the #rainforest-guardian checkbox is checked, I'd like to change the name attribute of #donation_amount to "a3".

This is the JS I'm trying to use to do this, but cannot work out what I'm doing wrong. Web Inspector is giving me "undefined is not a function" on the if statements.

<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>

I have seen several related posts on StackExchange and tried different things, e.g. click() vs. change() as shown above.

Please tell me what I'm doing wrong. Thanks in advance.

4

2 回答 2

1

您忘记了$一些 if 语句并错过了结束标签});

if (('#other-amount').prop('checked', true)) {

应该

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

尝试这个:

在这里演示

$(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");
        }
    });
});
于 2013-08-06T22:08:40.647 回答
0
 ('#other-amount').prop('checked', true)

应该

$('#other-amount').prop('checked')

('#rainforest_guardian').is(':checked')

应该

$('#rainforest_guardian').is(':checked')
于 2013-08-06T22:08:56.247 回答