在选中所有输入复选框和无线电组后,如何显示javascript确认消息?
需要显示确认消息后,点击提交按钮,但需要显示此窗口消息后,我的收音机和复选框被选中,如何做 HTML:在选中所有输入复选框和无线电组后,如何显示javascript确认消息?,javascript,jquery,html,forms,Javascript,Jquery,Html,Forms,需要显示确认消息后,点击提交按钮,但需要显示此窗口消息后,我的收音机和复选框被选中,如何做 HTML: 您可以检查特定的单选按钮组是否至少选择了一个单选按钮,如下所示: if ($(':radio[name="nameOfGroup"]:checked').length > 0) { // at least one radio button with the name nameOfGroup is checked } 因此,检查每个组,检查是否至少为该组选择了一个单选按钮,如果
您可以检查特定的单选按钮组是否至少选择了一个单选按钮,如下所示:
if ($(':radio[name="nameOfGroup"]:checked').length > 0)
{
// at least one radio button with the name nameOfGroup is checked
}
因此,检查每个组,检查是否至少为该组选择了一个单选按钮,如果是,则进入确认对话框:
var groups = {};
// get all of the unique radio button groups
$(':radio', form).each(function() {
// object names are unique
groups[this.name] = true;
});
var proceed = true;
$.each(groups, function(group, dummy) {
if ($(':radio[name="' + group + '"]:checked').length == 0)
{
// none of the radio buttons of this group are checked, abort
proceed = false;
// break out of the .each() loop
return false;
}
});
if (!proceed)
{
alert('Please check at least 1 radio button from each group!');
return false;
}
看这把小提琴:
(我的代码示例可能会有些简化,但为了清楚地说明发生了什么,我这样写了。)
(为任意团体编辑以解决OP的担忧)但我的表格中有几组收音机。使用不同的组名,例如此组[256]$(“:radio”)相当于$(“*:radio”)-使用$(“input:radio”)更有效-请参阅docs@pwdst如果将搜索范围限定到表单,该怎么办?i、 e.
$(':radio',form)
效率更低,使用输入前缀作为jQuery时,无论类型如何,仍然需要查看表单中的每个元素-在更窄的范围内效率更低。另外,除非表单是现有的JavaScript DOM元素变量或jQuery对象,否则不应该引用它吗?如果我正确地阅读文档,结果选择器可能看起来像$('input:radio',$('form')。我通常会写$('form')。find('input:radio'))
if ($(':radio[name="nameOfGroup"]:checked').length > 0)
{
// at least one radio button with the name nameOfGroup is checked
}
var groups = {};
// get all of the unique radio button groups
$(':radio', form).each(function() {
// object names are unique
groups[this.name] = true;
});
var proceed = true;
$.each(groups, function(group, dummy) {
if ($(':radio[name="' + group + '"]:checked').length == 0)
{
// none of the radio buttons of this group are checked, abort
proceed = false;
// break out of the .each() loop
return false;
}
});
if (!proceed)
{
alert('Please check at least 1 radio button from each group!');
return false;
}