I have these 4 checkboxes and I want to find them in jQuery by their names. Is there any way to find all 4 checkboxes by their name?
<input type="checkbox" value="1" id="RangeFlexionRightSide1" name="RangeFlexionRightSide">
<input type="checkbox" value="2" id="RangeFlexionRightSide2" name="RangeFlexionRightSide">
<input type="checkbox" value="1" id="ManualFlexionRightSide1" name="ManualFlexionRightSide">
<input type="checkbox" value="2" id="ManualFlexionRightSide2" name="ManualFlexionRightSide">
I am trying these below codes but I didn't get the result:
$("input[name^='Range Manual']").length
$("input[name^='Range,Manual']").length
$("input[name^='Range'][name^='Manual']").length
Yes you could use the ending by selector $=
like :
$("input:checkbox[name$='FlexionRightSide']").length
Or using start with selctor ^=
like :
$("input:checkbox[name^='Range'],input:checkbox[name^='Manual']").length
NOTE : You could give them a common class, and select them easely using class selector .
like :
$('.common_class').length
console.log($("input:checkbox[name$='FlexionRightSide']").length); //4 console.log($("input:checkbox[name^='Range'],input:checkbox[name^='Manual']").length); //4
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" value="1" id="RangeFlexionRightSide1" name="RangeFlexionRightSide"> <input type="checkbox" value="2" id="RangeFlexionRightSide2" name="RangeFlexionRightSide"> <input type="checkbox" value="1" id="ManualFlexionRightSide1" name="ManualFlexionRightSide"> <input type="checkbox" value="2" id="ManualFlexionRightSide2" name="ManualFlexionRightSide">
You can do that with *=
which will check if name contains value passed.
Or you can do as your way with ^=
as shown below.
In jQuery selectors
[name^='Range']
- Here ^=
find element whose name
starts with Range
. [name$='Range']
- Here $=
find element whose name
ends with Range
. [name*='Range']
- Here *=
find element whose name
contains Range
. Find more about jQuery Selector here.
console.log($("input[name^='Range'], input[name^='Manual']").length); console.log($("input[name*='RightSide']").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" value="1" id="RangeFlexionRightSide1" name="RangeFlexionRightSide"> <input type="checkbox" value="2" id="RangeFlexionRightSide2" name="RangeFlexionRightSide"> <input type="checkbox" value="1" id="ManualFlexionRightSide1" name="ManualFlexionRightSide"> <input type="checkbox" value="2" id="ManualFlexionRightSide2" name="ManualFlexionRightSide">
我想这就是您需要的:
$("input[name^='Range'],input[name^='Manual']").length
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.