首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除jquery所需的attr

删除jquery所需的attr
EN

Stack Overflow用户
提问于 2017-03-15 04:53:18
回答 1查看 1.4K关注 0票数 0

当我选中复选框时,是否可以从输入字段warranty_duration中删除所需的属性

代码语言:javascript
复制
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:

代码语言:javascript
复制
$('#livelong_warranty').change(function () {
if($(this).is(':checked') {
    $('#warranty_duration').removeAttr('required');
} else {
    $('#warranty_duration').attr('required');
}});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-15 05:16:06

我不得不将id属性添加到您的输入中,因为它们没有ID(您可以搜索名称,但我认为这不是您想要的)。Name主要用于通过HTML/PHP表单发送的post/get数据,而id则用于引用、样式以及JavaScript元素。

代码语言:javascript
复制
$('#livelong_warranty').on('change', function () {
	$('#warranty_duration').attr('required',$('#livelong_warranty').is(":checked"));
	console.log($('#warranty_duration').attr('required'));
});
代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42796336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档