Javascript 如何在jquery中更改属性“隐藏”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30808723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change attribute "hidden" in jquery
提问by user3314813
<td>
<input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>">
</td>
<td id="delete" hidden="true">
the file will be deleted from the newsletter
</td>
I want to know how can i change the attribute "hidden" to false in Jquery when the checkbox is checked or not checked.
我想知道如何在选中或未选中复选框时将 Jquery 中的“隐藏”属性更改为 false。
回答by AmmarCSE
$(':checkbox').change(function(){
$('#delete').removeAttr('hidden');
});
Note, thanks to tip by A.Wolff, you should use removeAttrinstead of setting to false. When set to false, the element will still be hidden. Therefore, removing is more effective.
请注意,感谢 tip by A.Wolff,您应该使用removeAttr而不是设置为 false。当设置为 false 时,元素仍将被隐藏。因此,去除更有效。
回答by Anoop Joshi
You can use jquery attr method
您可以使用 jquery attr 方法
$("#delete").attr("hidden",true);
回答by JoeFlash
A. Wolff was leading you in the right direction. There are several attributes where you should not be setting a string value. You must toggle it with a boolean trueor false.
A. Wolff 将您引向了正确的方向。有几个属性不应设置字符串值。您必须使用布尔值true或false.
.attr("hidden", false)will remove the attribute the same as using .removeAttr("hidden").
.attr("hidden", false)将删除与使用相同的属性.removeAttr("hidden")。
.attr("hidden", "false")is incorrect and the tag remains hidden.
.attr("hidden", "false")不正确,标签保持隐藏状态。
You should not be setting hidden, checked, selected, or several others to any string value to toggle it.
你不应该设置hidden,checked,selected,或一些其他任何字符串值切换它。
回答by Pranav C Balan
Use prop()for updating the hidden property, and change()for handling the change event.
使用prop()更新的隐藏属性,change()用于处理更改事件。
$('#check').change(function() {
$("#delete").prop("hidden", !this.checked);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>">
</td>
<td id="delete" hidden="true">
the file will be deleted from the newsletter
</td>
</tr>
</table>

