2011-10-31 177 views
0

我想問用戶一系列的問題,其答案是單選按鈕,並且每個答案都會更新id = creditamount的html變量creditAmount。我的代碼只在點擊第二個單選按鈕後才起作用?任何想法爲什麼會發生?jquery:單選按鈕的onclick函數沒有點擊第一次單選按鈕

<!DOCTYPE html> 
    <html> 
    <head> 
    <script type="text/javascript" src="jquery-1.4.2.js"></script> 
    <link rel = "stylesheet" type="text/css" href="custom.css" /> 

    <script> 
    var creditAmount; 
    $(document).ready(function(){ 

     $("input[name='alive']").live('click', function(){ 
     $("input").click(function() { 

     creditAnswer = $(":checked").val(); 
     if (creditAnswer == "Yes") 
      { 
       creditAmount = 1000; 
      }  
     else if (creditAnswer == "No") 
      { 
       creditAmount = 100; 
      }  
     else if (creditAnswer == "Both") 
      { 
       creditAmount = 5000; 
      }  
     $("#creditamount").html(creditAmount); 
     }); 
    });    
     }); 
     </script> 

    </head> 
    <form> 
    <div id="creditlimit"> Credit Limit: </div><div id ="creditamount">0 </div> 

    Are you currently alive? 
    <input type="radio" name="alive" value="Yes" /> Yes 
    <input type="radio" name="alive" value="No" /> No 
    <input type="radio" name="alive" value="Both" /> Depends upon the day of the week. 
    </form> 
    </html> 

回答

1

您只綁定第一次點擊後點擊事件的實際業務邏輯。

嘗試

<script> 
    var creditAmount; 
    $(document).ready(function(){ 

     $("input[name='alive']").live('click', function(){ 

     creditAnswer = $(":checked").val(); 
     if (creditAnswer == "Yes") 
      { 
       creditAmount = 1000; 
      }  
     else if (creditAnswer == "No") 
      { 
       creditAmount = 100; 
      }  
     else if (creditAnswer == "Both") 
      { 
       creditAmount = 5000; 
      }  
     $("#creditamount").html(creditAmount); 



    });    
     }); 
     </script> 
+0

完美的作品。謝謝。 – ponzicoder