选中所有必需的单选按钮时启用按钮

时间:2013-06-13 18:04:45

标签: php javascript jquery

我需要在选择所有单选按钮(必需)时启用按钮。我知道如何使用固定数量的选项(单选按钮,是或否)。问题是表单是使用php动态生成的,并且需要用户使用单选按钮回答是或否的“问题”数量是未知的。

这是一个生成的html示例代码:

<form>
  Generic Privacy Policy
  [*]static global opt1
  <input type="radio" name="ppopt1" value="0" data-req="1"> No
  <input type="radio" name="ppopt1" value="1" data-req="1"> Yes
  [*]static global opt2
  <input type="radio" name="ppopt2" value="0" data-req="1"> No
  <input type="radio" name="ppopt2" value="1" data-req="1"> Yes
  static global opt3
  <input type="radio" name="ppopt3" value="0" data-req="0"> No
  <input type="radio" name="ppopt3" value="1" data-req="0"> Yes
  static global opt4
  <input type="radio" name="ppopt4" value="0" data-req="0"> No
  <input type="radio" name="ppopt4" value="1" data-req="0"> Yes
  <button name="btn" type="button" disabled>Send!</button>
</form>

正如您所见,我使用 data-req 来指定问题是否需要和回答(并且它也需要是Yes value = 1)。

如何(可能使用jquery)我可以在条件达到时启用按钮 btn

PS。我完全控制了PHP代码,所以如果你认为对php代码的一些修改可以简化前端编码可以自由建议。

5 个答案:

答案 0 :(得分:1)

以下是我如何去做的事情:

$(document).ready(function () {
    var $form = $("form"),
        $button = $form.find("button"),
        $radios = $form.find('input[type="radio"]'),
        $requiredRadios = $radios.filter('[data-req="1"]');

    $requiredRadios.on("click", function () {
        var numValid = 0;

        $requiredRadios.each(function () {
            var $this = $(this),
                myName = $this.attr("name"),
                isYes = ($this.val() === "1"),
                $target = (isYes ? $this : $radios.filter('input[name="' + myName + '"][value="1"]'));

            if ($target.prop("checked")) {
                numValid++;
            }
        });

        if (numValid === $requiredRadios.length) {
            $button.prop("disabled", false);
        } else {
            $button.prop("disabled", true);
        }
    });
});

DEMO: http://jsfiddle.net/aCheQ/1/

它找到所有必需的单选按钮,并将click处理程序绑定到每个按钮。单击一个时,浏览所有必需的单选按钮,并计数以查看是否单击了足够的“是”单选按钮。如果是这样,它启用按钮,否则禁用它。我确信它可以清理一下,或者提高效率,但它似乎是预期的。

答案 1 :(得分:1)

$(function() {
    $("input[type='radio']").click(function(){
        $requires = $('input:radio[data-req=1][value=1]');

        var checked = 0;
        $requires.each(function () {
            if($(this)[0].checked)
                $("button").attr("disabled", (++checked < $requires.length));
        });
    });
});

jsFiddle code

答案 2 :(得分:0)

经过全面测试的代码并正常工作

    $("input[type='radio']").click(function(){
    var enableButton = false;
    var totalChecked = 0;
  var reqElements  =  $('input:radio[data-req =1]');
    var reqElementsCount= $("input:radio[data-req =1][value=1]").size();
    $(reqElements).each(function (index, ele) {
        if($(ele).is(":checked") && ele.value === "1"){
           totalChecked++;
        }
    });
    if(totalChecked == reqElementsCount){ 
        $("button").removeAttr("disabled");
    }
    else{
         $("button").attr("disabled",true);
    }
});

demo at JSFiddle

答案 3 :(得分:0)

只需添加以下javascript,代码注释中有解释

$('input[type="radio"]').change(function ()
{
   var req,reqYes;
   req=$('input[type="radio"][data-req="1"][value="1"]').length;
   reqYes=$('input:checked[type="radio"][data-req="1"][value="1"]').length;
   document.getElementsByName("btn")[0].disabled= (req!=reqYes);
   return true;
 }
);

/* - Is onchange because it happens after the click once the radiobutton state has been changed 
   - Is getElementsByName()[0] because you used the name instead the ID (The id is unique, but not the name so it returns an array of objects)
   - No need to do a foreach since you can check the Radiosbutton required and the Required with yes answer, the foreach is done by Jquery
**/

摆弄http://jsfiddle.net/egorbatik/pVh4U/

答案 4 :(得分:0)

如果每个组只包含是/否,那么您可以检查以确保检查的是无线电的数量是所需总无线电的一半

$(function () {
    var req = $('input[data-req=1]');
    req.change(function () {
        // if the checked radios with value=1 amount == half of the total required (because only Yes or No can be checked)
        var allchecked = req.filter('[value=1]:checked').length == req.length/2
        // disable of not all checked - enable if all checked
        $('button').prop('disabled', !allchecked);
    });
});

FIDDLE

如果你想检查组的数量,你可以循环并获得不同的输入名称

$(function () {
    var req = $('input[data-req=1]');
    var groups = [];
    req.each(function(i,v){
        // if group name not in array add it
        if($.inArray(v.name,groups) == -1){
            groups.push(v.name);
        }
    });
    req.change(function () {
        // if the checked amount == number of groups
        var allchecked = req.filter('[value=1]:checked').length == groups.length;
        // disable of not all checked - enable if all checked
        $('button').prop('disabled', !allchecked);
    });
});

FIDDLE

想到这一点 - 你也可以只用data-req = 1和value = 1

来检查检查YES的数量与元素的数量。
$(function () {
    var req = $('input[data-req=1]');
    req.change(function () {        
        var allchecked = req.filter('[value=1]:checked').length == req.filter('[value=1]').length;
        $('button').prop('disabled', !allchecked);
    });
});

FIDDLE

相关问题