当我选中复选框时,是否可以从输入字段warranty_duration中删除所需的属性
years warrenty<label id="warrantyDurationlabel"><input name="warranty_duration" type="number" placeholder="aantal jaren garantie" min="1" size="25" required /></label>
levenlang<label id="livelongwarranty"><input name="livelong_warranty" type="checkbox" onclick="js/warrantyrequired.js" /></label>我有以下jquery:
$('#livelong_warranty').change(function () {
if($(this).is(':checked') {
$('#warranty_duration').removeAttr('required');
} else {
$('#warranty_duration').attr('required');
}});发布于 2017-03-15 05:16:06
我不得不将id属性添加到您的输入中,因为它们没有ID(您可以搜索名称,但我认为这不是您想要的)。Name主要用于通过HTML/PHP表单发送的post/get数据,而id则用于引用、样式以及JavaScript元素。
$('#livelong_warranty').on('change', function () {
$('#warranty_duration').attr('required',$('#livelong_warranty').is(":checked"));
console.log($('#warranty_duration').attr('required'));
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
years warrenty<label id="warrantyDurationlabel"><input name="warranty_duration" id="warranty_duration" type="number" placeholder="aantal jaren garantie" min="1" size="25" required /></label>
levenlang<label id="livelongwarranty"><input name="livelong_warranty" id="livelong_warranty" type="checkbox" /></label>
https://stackoverflow.com/questions/42796336
复制相似问题