简体   繁体   English

删除jQuery所需的attr

[英]remove attr required with jquery

can I remove the attr required from input field warranty_duration when I check the checkbox livelongwarranty 选中复选框livelongwarranty时,可以从输入字段保修时间中删除必需的属性吗?

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>

I have the following jquery: 我有以下jQuery:

$('#livelong_warranty').change(function () {
if($(this).is(':checked') {
    $('#warranty_duration').removeAttr('required');
} else {
    $('#warranty_duration').attr('required');
}});

I had to add id attributes to your inputs because they had no ID's (you can search for names, but I don't think that's what you want). 我必须在输入中添加id属性,因为它们没有ID(您可以搜索名称,但我认为这不是您想要的)。 Name is used primarily for post/get data sent via an HTML/PHP form and id is used to reference, style, and further more JavaScript an element. Name主要用于通过HTML / PHP表单发送的发布/获取数据,而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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM