2013-07-28 20 views
0

這是我 My JSFiddle驗證的條款和條件複選框

我試圖添加一個類被點擊複選框的時候,雖然我似乎無法弄清楚如何做到這一點的方式,將適合到我已有的代碼中。

CODE:

$(this).live("change keyup", function() { 

    var checkbox = $('#termscons').prop(':checked'); 

    if (currid == "termcons") { 

       if (checkbox == 'checked') { 
        infobox.html("Accepted!"); 
        infobox.addClass('good'); 
       } else if (checkbox !== 'checked') { 
        infobox.html(replacehtml); 
        infobox.removeClass('good'); 
       } 
      } 

     if ($('#termscons').next().hasClass('good')) { 

      $("#sendbtn").css("display", "block"); 
     } else { 
      $("#sendbtn").css("display", "none"); 

     } 
+0

只是一個想法:確保他們已經滾動一路下跌是不真的去對UX的一種方式。在接受反對UX之前讓他們等待1分鐘。 –

回答

3

首先,不使用live,它的棄用,改用on,例如

$(document).on('change', '#termscons', function(){ 
    if($(this).is(':checked')) { 
     // checked, so you can add class, i.e. 
     infobox.addClass('good'); 
     // other code 
    } 
    else { 
     // not checked 
     infobox.removeClass('good'); 
     // other code 
    } 
}); 

這也回答了,你怎麼可以添加/刪除class取決於checkbox的狀態。

DEMO.

+0

http://jsfiddle.net/gUCcd/5/這是我正在使用的整個代碼。我將如何實現它呢?那時我剛剛走了。 – Desolate

0

你在做什麼試圖訪問的:checked支柱應該是checked 我複製這從一個網站 http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html

// First way 
    $('#checkBox').attr('checked'); 
    // If using jQuery 1.6 or up use .prop() instead 
    // Learn the difference between property and an attribute 
    $('#checkBox').prop('checked'); 

    // Second way 
    $('#edit-checkbox-id').is(':checked'); 

    // Third way for jQuery 1.2 
    $("input[@type=checkbox][@checked]").each( 
     function() { 
      // Insert code here 
     } 
    ); 
    // Third way == UPDATE jQuery 1.3 
    $("input[type=checkbox][checked]").each( 
     function() { 
      // Insert code here 
     } 
    ); 

    // In his comment Andy mentions that the 3rd method 
    // does not work in Firefox 3.5.2 & jQuery 1.3.2 

我希望這能幫助:)

+0

我已經使用了很多方法,我已經完成{checked}和{:checked}都沒有爲我工作。 – Desolate

+0

請只提供複選框的提琴,所以我們可以幫助更多,因爲代碼有太大:) – Sedz

+0

http://jsfiddle.net/gUCcd/6/請檢查此版本請 – Sedz

0

你有幾個問題。工作jsfiddle

  1. 您綁定的所有文本框的焦點事件,然後,在處理程序中,您嘗試綁定到當前文本框的更改事件,期待您的複選框是文本框的一個。)
  2. 你綁定到錯誤的事件的複選框。使用click
  3. 檢查prop(「:checked」)是錯誤的。改爲使用is

    $("#termscons").click(function(){ 
          var infobox = $(this).next(); 
          var checkbox = $(this).is(':checked'); 
    
          // we use this logic to check the subject 
          if (checkbox) { 
           infobox.html("Accepted!"); 
           infobox.addClass('good'); 
          } else { 
           infobox.html(""); 
           infobox.removeClass('good'); 
          } 
    }); 
    
+0

你做了什麼,爲什麼解決這個問題? –

+0

你的JSFiddle不適合我嗎? – Desolate