Jquery:单击单选按钮时,单选按钮的onclick功能不会触发

时间:2011-10-31 06:35:53

标签: jquery

我想问用户一系列问题,其答案是单选按钮,每个答案都会使用变量creditAmount更新id = creditamount的html。我的代码仅在我点击第二个单选按钮后才有效?任何想法为什么会发生这种情况?

<!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 个答案:

答案 0 :(得分: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>