The jquery attr method gets the checked property of the input
- 2020-03-30 03:07:25
- OfStack
Problem: it is common to use the attr method of jQuery plug-in to get the checked property value. The size of the obtained value is undefined. At this time, prop method can be used to get the true value.
1. Get the checked property by prop method, and the checked return value is Boolean, check true, or flase
$("#selectAll").attr("checked") will return undefined if the undefined checked property is initialized in the current input when the method attr is used.
If the checked property is initialized in the current input, $("#selectAll").attr("checked") will return checked whether or not it is selected.
To sum up, if you use jquery, you should use the prop method to get and set checked properties, not attr.
1. Get the checked property by prop method, and the checked return value is Boolean, check true, or flase
<input type="checkbox" id="selectAll" onclick="checkAll()"> Future generations
function checkAll()
{
var checkedOfAll=$("#selectAll").prop("checked");
alert(checkedOfAll);
$("input[name='procheck']").prop("checked", checkedOfAll);
}
$("#selectAll").attr("checked") will return undefined if the undefined checked property is initialized in the current input when the method attr is used.
<input type="checkbox" id="selectAll" onclick="checkAll()" > Future generations
If the checked property is initialized in the current input, $("#selectAll").attr("checked") will return checked whether or not it is selected.
<input type="checkbox" id="selectAll" onclick="checkAll()" checked> Future generations
function checkAll()
{
var checkedOfAll=$("#selectAll").attr("checked");
alert(checkedOfAll);
$("input[name='procheck']").attr("checked", checkedOfAll);
}
To sum up, if you use jquery, you should use the prop method to get and set checked properties, not attr.