Checkbox and select related operations

1 The select drop-down box option is not selected, and the event needs to be monitored through the onchange event of select

<select name="myselect" id="myselect">
    <option value="opt1">选项1</option>
    <option value="opt2">选项2</option>
    <option value="opt3">选项3</option>
</select>
 
$("#myselect").change(function(){
    var opt=$("#myselect").val();
    ...
});

  

2 Assignment and value of checkbox 

1. Get a single checkbox selected item (three ways of writing):

$("input:checkbox:checked").val()
//or
$("input:[type='checkbox']:checked").val();
//or
$("input:[name='ck']:checked").val();

2. Get multiple checkbox selections:

$('input:checkbox').each(function() {
    if ($(this).attr('checked') ==true) {
        alert($(this).val());
    }
});

3. Set the first checkbox to the checked value:

$('input:checkbox:first').attr("checked",'checked');
$('input:checkbox').eq(0).attr("checked",'true');

4. Set the last checkbox as the selected value:

$('input:radio:last').attr('checked', 'checked');
$('input:radio:last').attr('checked', 'true');

5. Set any checkbox as the selected value according to the index value:

$('input:checkbox).eq(index value).attr('checked', 'true');index value=0,1,2....
$('input:radio').slice(1,2).attr('checked', 'true');  

6. Select multiple checkboxes: 
select the first and second checkboxes at the same time:

$('input:radio').slice(0,2).attr('checked','true');

7. Set the checkbox as the selected value according to the Value value:

$("input:checkbox[value='1']").attr('checked','true');

8. Delete the checkbox with Value=1:

$("input:checkbox[value='1']").remove();

9. Delete the first few checkboxes:

$("input:checkbox").eq(index value).remove(); index value=0,1,2....
//If delete the 3rd checkbox:
$("input:checkbox").eq(2).remove();

10. Traverse the checkbox:

$('input:checkbox').each(function (index, domEle) {
  // write code
});

11. Check All

$('input:checkbox').each(function() {
    $(this).attr('checked', true);
});

12. Deselect all:

$('input:checkbox').each(function () {
    $(this).attr('checked',false);
});

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326268432&siteId=291194637