2011-10-31 136 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