我如何禁用和启用输入类型提交,一旦我检查了一个输入类型单选
How do I disable and enable back the input type submit, once I have checked one input type radio?
我希望标题能说明一切。我有一个表单,有很多组的输入类型单选,我希望输入类型提交按钮被禁用,直到一个输入类型单选从每个组已被检查。这个例子只是一组输入类型的无线电,但它显然不能工作。我错过了什么?
https://jsfiddle.net/Suiberu/70tkgk5t/var current = $('input.class_x').filter(':checked');
var sbmtBtn = document.getElementById('SubmitButton');
sbmtBtn.disabled = true;
if (current.length > 0){
sbmtBtn.disabled = false;
}
else{
sbmtBtn.disabled = true;
}
谢谢!
您发布的代码的问题是,您运行脚本时,页面加载,在这一点上有& &;在你的演示中没有选定的无线电输入。要使该代码工作,您只需要将其包装在无线电<input>
元素的change
事件的事件处理程序中:
// binds the anonymous function of the on() method to act
// as the event-handler for the 'change' event triggered
// on the <input> elements of type=radio:
$('input[type=radio]').on('change', function() {
var current = $('input.class_x').filter(':checked');
var sbmtBtn = document.getElementById('SubmitButton');
sbmtBtn.disabled = true;
if (current.length > 0) {
sbmtBtn.disabled = false;
} else {
sbmtBtn.disabled = true;
}
// fires the 'change' event when the script is
// first run, which sets the disabled property
// of the submit-button appropriately:
}).change();
$('input[type=radio]').on('change', function() {
var current = $('input.class_x').filter(':checked');
var sbmtBtn = document.getElementById('SubmitButton');
sbmtBtn.disabled = true;
if (current.length > 0) {
sbmtBtn.disabled = false;
} else {
sbmtBtn.disabled = true;
}
}).change();
input {
display: block;
margin: 0.5em 0;
}
input[type='submit']:disabled {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="myform" autocomplete="off" method="post">
<input class="class_x" type="radio" name="name_x" value="value_1" id="id_1" />
<input class="class_x" type="radio" name="name_x" value="value_2" id="id_2" />
<input class="class_x" type="radio" name="name_x" value="value_3" id="id_3" />
<input type="submit" name="name_submit" value="OK" class="class_submit" id="SubmitButton" required/>
</form>
JS Fiddle demo.
要处理多组无线电输入,一种低效的方法是简单地使用额外的类,在选择器中指定组名来识别这些组。
这种方法效率很低,因为您需要提前知道每组元素的标识符,以及将有多少个元素(因为您最初硬编码了一些必须在if
语句中检查的元素)。
$('input[type=radio]').on('change', function() {
var current = $('input.class_x, input.class_y, input.class_z').filter(':checked');
var sbmtBtn = document.getElementById('SubmitButton');
sbmtBtn.disabled = true;
if (current.length > 2) {
sbmtBtn.disabled = false;
} else {
sbmtBtn.disabled = true;
}
}).change();
input {
display: block;
margin: 0.5em 0;
}
input[type='submit']:disabled {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="myform" autocomplete="off" method="post">
<fieldset>
<input class="class_x" type="radio" name="name_x" value="value_1" />
<input class="class_x" type="radio" name="name_x" value="value_2" />
<input class="class_x" type="radio" name="name_x" value="value_3" />
</fieldset>
<fieldset>
<input class="class_y" type="radio" name="name_y" value="value_1" />
<input class="class_y" type="radio" name="name_y" value="value_2" />
<input class="class_y" type="radio" name="name_y" value="value_3" />
</fieldset>
<fieldset>
<input class="class_z" type="radio" name="name_z" value="value_1" />
<input class="class_z" type="radio" name="name_z" value="value_2" />
<input class="class_z" type="radio" name="name_z" value="value_3" />
</fieldset>
<input type="submit" name="name_submit" value="OK" class="class_submit" id="SubmitButton" required/>
</form>
因此,为了避免这种低效率,我们都希望代码本身能够给定一个简单的选择器,如$('input[type=radio]')
,确定有多少组,因此必须检查多少元素才能满足"每组检查一个"的标准。
幸运的是,下面的代码就是这样做的:
// we need to use this collection within the event-handler,
// so we're caching it here:
var allRadios = $('input[type=radio]');
// binding the anonymous function as the event-handler for
// the 'change' event, as before:
allRadios.on('change', function() {
// retrieving the names of all radio-inputs on the page,
// using map():
var groups = allRadios.map(function() {
// returning the 'name' property of each of the
// radio-inputs to the created map:
return this.name;
// converting the map into an Array:
}).get(),
// creating an empty Array to hold the unique
// group-names:
uniqueGroupNames = [];
// iterating over the groups Array using the
// Array.prototype.forEach() method:
groups.forEach(function(name) {
// 'name' is a reference to the current Array-element
// of the Array over which we're iterating.
// the name (the current Array-element) is not found
// within the uniqueGroupNames Array:
if (uniqueGroupNames.indexOf(name) === -1) {
// then we add it to that Array:
uniqueGroupNames.push(name)
}
});
// here we find the submit-button, and use the prop()
// method to set its 'disabled' property, using a
// conditional (ternary) operator:
$('input[type=submit]').prop('disabled',
// we find the number of checked radio-inputs and if
// that number is less than the number of unique group
// names the condition evaluates to true and the disabled
// property is sset to true; if the number of checked
// radio-inputs is not less-than the number of group names,
// the condition is false, and the disabled property is set
// to false:
allRadios.filter(':checked').length < uniqueGroupNames.length
);
}).change();
var allRadios = $('input[type=radio]');
allRadios.on('change', function() {
var groups = allRadios.map(function() {
return this.name;
}).get(),
uniqueGroupNames = [];
groups.forEach(function(name) {
if (uniqueGroupNames.indexOf(name) === -1) {
uniqueGroupNames.push(name)
}
});
$('input[type=submit]').prop('disabled', allRadios.filter(':checked').length < uniqueGroupNames.length);
}).change();
input {
display: block;
margin: 0.5em 0;
}
input[type='submit']:disabled {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="myform" autocomplete="off" method="post">
<fieldset>
<input class="class_x" type="radio" name="name_x" value="value_1" />
<input class="class_x" type="radio" name="name_x" value="value_2" />
<input class="class_x" type="radio" name="name_x" value="value_3" />
</fieldset>
<fieldset>
<input class="class_y" type="radio" name="name_y" value="value_1" />
<input class="class_y" type="radio" name="name_y" value="value_2" />
<input class="class_y" type="radio" name="name_y" value="value_3" />
</fieldset>
<fieldset>
<input class="class_z" type="radio" name="name_z" value="value_1" />
<input class="class_z" type="radio" name="name_z" value="value_2" />
<input class="class_z" type="radio" name="name_z" value="value_3" />
</fieldset>
<input type="submit" name="name_submit" value="OK" class="class_submit" id="SubmitButton" required/>
</form>
JS Fiddle demo.
具有:
- CSS:
-
:checked
伪类
-
- JavaScript:
-
Array.prototype.forEach()
. - 条件(三元)运算符。
-
- jQuery:
-
change()
. -
Filter()
. -
get()
. -
map()
. -
on()
. -
prop()
.
-
看到更新的小提琴
$(document).ready(function () {
var sbmtBtn = document.getElementById('SubmitButton');
sbmtBtn.disabled = true;
$('.class_x').click(function (){
if ($('.class_x').is(':checked')){//if only allow any on of the radio checked
sbmtBtn.disabled= false;
}
else{
sbmtBtn.disabled = true;
}
})
});
您可以使用此代码获取选定类型,
$('#check_id').on('change', '[name=cust_check]', function() {
checkValues = $('input[name=cust_check]:checked').map(function()
{
return $(this).attr('value');
}).get();
});
相关文章:
- 使用模式格式化Number类型输入中的值
- 在 JavaScript 中单击函数时两个日期类型输入之间的差异的代码
- 在codeigneter中按类型输入框显示数据库中的数据
- 已从'上传文件;文件'img字段中未显示类型输入字段
- 如何使用ReactTestUtils设置HTML5文件类型输入
- 检测用户拖动范围类型输入
- 无法在使用 Java 脚本创建的表单中设置提交类型输入
- 按钮类型输入上的点击事件不起作用
- 如何使用javascript更改隐藏类型输入字段的值
- sap.m中的自定义输入类型.输入sap-ui5
- 如何使用jquery动态添加/删除文件类型输入字段
- 提交事件未捕获提交类型输入
- Jquery - info/docs关于适用于表单中不同类型输入元素的属性列表
- 从Mobile Safari'日期/时间类型输入字段触发哪些事件?
- 饼状图II -动态值从范围类型输入
- 我可以在文件类型输入中显示文件大小吗?
- 新的webkit在数字类型输入中将十进制逗号转换为~ dot,反之亦然.该浏览器特性的Javascript名称
- 如何在javascript中验证具有相同名称的多个相同类型输入
- 邮件类型输入:如果邮件错误或正确,动态改变按钮的颜色/文本
- 如何将文件类型输入传递到javascript中,并将其用于另一个php页面