2013-08-06 49 views
0

我有一組單選按鈕,一個文本框,一個複選框和一個隱藏文本框。使用if語句檢查輸入時修改屬性屬性

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

當最後一個單選按鈕#other-amount被選中時,我想啓用#other-amount-val文本字段。

如果選中#rainforest-guardian複選框,我想將#donation_amount的名稱屬性更改爲「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> 

我在StackExchange上看到了幾個相關的帖子,並嘗試了不同的東西,例如,點擊()與更改()如上所示。

請告訴我我做錯了什麼。提前致謝。

+1

你。就緒未關閉 – r043v

回答

1

你忘了一些,如果語句的$,錯過了結束標記});

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

應該

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

試試這個:

DEMO HERE

$(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"); 
     } 
    }); 
}); 
+0

啊。愚蠢的錯誤。謝謝Sergio和@Musa。 錯誤已排序,但第二個函數不會更改輸入上的name屬性。你能說出原因嗎? –

+0

啊。它在小提琴上。只是不在我的本地環境。感謝您的幫助。 –

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

應該

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

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

應該

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