[英]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.