我有一个可以出现在不同页面上的表单,根据情况,这些字段并不总是必需的。
我有隐藏 div 的代码,但需要不再需要输入字段。使用 HTML5 Required 属性。无法使代码正常工作。代码如下:
$('#detailssame').click(function() {
if($(this).is(':checked')) {
$(\"#detailssamehide\").hide();
} else {
$(\"#detailssamehide\").show();
}
});
$('.fullboxform input[type=text], .fullboxform select').each(function() {
$(this).removeAttr('required');
});
非常感谢所有帮助。
事实证明上面的代码确实工作正常,但是使用 prop 可以得到替代答案
最佳答案
尝试使用 prop()用于设置 HTML5 属性的函数。
$(function () {
$('#detailssame').click(function() {
var checked = $(this).is(':checked');
if(checked) {
$("#detailssamehide").hide();
} else {
$("#detailssamehide").show();
}
$('.fullboxform input[type=text], .fullboxform select').each(function() {
$(this).prop('required', !checked);
});
});
});
关于jquery - 单击时,隐藏 div 并删除所需的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887462/