【问题标题】:Validation for Terms and Conditions check box验证条款和条件复选框
【发布时间】:2013-07-28 01:28:08
【问题描述】:

这是我的 My JSFiddle

我正在尝试在单击复选框时添加一个类,但我似乎无法弄清楚如何以适合我已经拥有的代码的方式进行添加。

代码:

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

        }

【问题讨论】:

  • 只是一个想法:确保他们一直向下滚动是一种并不真正违背用户体验的方式。让他们等待,比如 1 分钟,然后接受不利于用户体验。

标签: jquery forms validation checkbox


【解决方案1】:

首先,不要使用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
    }
});

这也回答了如何根据checkbox 的状态添加/删除class

DEMO.

【讨论】:

  • jsfiddle.net/gUCcd/5 这是我正在使用的全部代码。我将如何实现它?我刚才去了。
【解决方案2】:

你正在做的是试图访问 :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

我希望这可以帮助:)

【讨论】:

  • 我已经尝试过使用我做过的很多方法 {checked} 和 {:checked} 都没有为我工作。
  • 请为复选框制作一个小提琴,以便我们可以提供更多帮助,因为那里的代码太大:)
  • 抱歉 Sedz 不得不出来工作。这几乎就是我想要它做的,但它不必显示信息。
  • Hey Sedz console.log 从浏览器获取信息,所以它只会在第二次点击时添加类好,因为它首先需要信息。我需要它,以便它在第一次点击时。那我会用什么来实现这一点?
【解决方案3】:

你有几个问题。工作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');
                }
    });
    

【讨论】:

  • 你做了什么,为什么能解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 2015-12-06
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
相关资源
最近更新 更多