简体   繁体   English

禁用提交按钮,直到选中一组动态创建的单选按钮中的一个

[英]Disable submit button until one in a group of dynamically-created radio buttons selected

I would like to disable a submit button until one of a group of radio buttons is selected. 我想禁用提交按钮,直到选择了一组单选按钮之一。 I know there are similar questions out there, but none pertain to a dynamically-created group of radio buttons... 知道那里也有类似的问题,但都与动态创建的单选按钮组无关。

Here is what I have.. a script at the top of the page generates a number of buttons given a user upload in a previous view: 这就是我所拥有的..页面顶部的脚本为用户在上一个视图中上传提供了许多按钮:

var jScriptArray = new Array(@ViewBag.ColNames.Length);
var array = @Html.Raw(Json.Encode(ViewBag.ColNames));
for( var i = 0; i < @ViewBag.ColNames.Length; i++ ) {
    jScriptArray[i] = array[i];
}
var length = @(ViewBag.NCols);
$(document).ready(function () {
     for (var i = 0; i < length; i++) {
           $('#radioGroupBy').append('<input id="grp' + i +'" type="radio" name="group" value="'+i+'">'+jScriptArray[i]+'</input>')
           $('#radioGroupBy').append('<p style="padding:0px;margin:0px;"></br></p>');
     }
});

This works, and selecting any of the buttons returns the proper value; 这有效,选择任何按钮都将返回正确的值; great. 大。 However, I want to disable the submit button until one of these radio buttons is selected. 但是,我想禁用提交按钮,直到选择了这些单选按钮之一。 Using an answer I found on SO earlier, I created the following (this works, but only if I hard code the group of buttons. The issue is it won't work with the Javascript-created group): 使用我之前在SO上找到的答案,我创建了以下内容(此方法有效,但仅当我对按钮组进行硬编码时才起作用。问题是它不适用于Javascript创建的组):

var $radioButtons = $("input[name='group']");
    $radioButtons.change(function () {
        var anyRadioButtonHasValue = false;
        // iterate through all radio buttons
        $radioButtons.each(function () {
            if (this.checked) {
                // indicate we found a radio button which has a value
                anyRadioButtonHasValue = true;
                // break out of each loop
                return false;
            }
        });
        // check if we found any radio button which has a value
        if (anyRadioButtonHasValue) {
            // enable submit button.
            $("input[name='submitbtn']").removeAttr("disabled");
        }
    });

Also, for the sake of thoroughness, here is the submit button: 另外,为了透彻,这里是提交按钮:

<input id="submitbtn" name="submitbtn" type="submit" value="Drill Down" disabled="disabled" />

Thanks so much! 非常感谢!

Event delegation (also, use .prop() when removing the disabled property to the submit button) 事件委托(此外,将disabled属性删除到“提交”按钮时,请使用.prop()

$("#radioGroupBy").on("change", ":radio[name=group]", function() {
    var $radioButtons = $(":radio[name=group]");
    var anyRadioButtonHasValue = false;
    // iterate through all radio buttons
    $radioButtons.each(function () {
        if (this.checked) {
            // indicate we found a radio button which has a value
            anyRadioButtonHasValue = true;
            // break out of each loop
            return false;
        }
    });
    // check if we found any radio button which has a value
    if (anyRadioButtonHasValue) {
        $("input[name='submitbtn']").prop("disabled", false);
    }
});

I figured it out. 我想到了。 As Benjamin suggested in comments above, the latter script was executing before the DOM was ready. 正如本杰明在上述评论中所建议的那样,后一个脚本是在DOM准备就绪之前执行的。 I solved it by just surrounding the whole script in $(window).load... : 我通过将整个脚本放在$(window).load ...中来解决它:

$(window).load(function () {
    var $radioButtons = $("input[name='group']");
    $radioButtons.change(function () {
        var anyRadioButtonHasValue = false;
        // iterate through all radio buttons
        $radioButtons.each(function () {
            if (this.checked) {
                // indicate we found a radio button which has a value
                anyRadioButtonHasValue = true;
                // break out of each loop
                return false;
            }
        });
        // check if we found any radio button which has a value
        if (anyRadioButtonHasValue) {
            // enable submit button.
            $("input[name='submitbtn']").removeAttr("disabled");
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化动态创建的btn组单选按钮 - Initialising a dynamically-created btn-group of radio buttons 如果已选择一个单选按钮,如何禁用一组单选按钮,如果未选择一个单选按钮并单击下一个按钮,如何显示警报 - How to disable a group of radio buttons if one has been selected and display an alert if one is not selected and the next button is clicked 禁用按钮,直到选择了AJAX生成的单选按钮 - Disable button until AJAX generated radio buttons have been selected 禁用单选按钮,直到单击按钮 - Disable Radio Buttons Until Button Clicked 如何在选择单选按钮之前禁用提交按钮(Javascript、HTML 和 Flask) - How to disable a submit button until a radio button is selected (Javascript, HTML, and Flask) 启用一个无线电组的单选按钮,并在选中时禁用另一个无线电组 - To enable Radio button of one radio group and disable the other radio group when selected 基于单选按钮启用/禁用提交按钮 - Enable/Disable Submit Button based on radio buttons 禁用按钮,直到单击一个单选按钮 - Disable button until one radio button is clicked 如何在选择文件之前禁用提交按钮 - How to disable submit button until file is selected 禁用按钮提交,直到选择了下拉列表 - Disable button submit until dropdownlist been selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM