Home  >  Article  >  Web Front-end  >  Summary of common control methods for jQuery operation forms_jquery

Summary of common control methods for jQuery operation forms_jquery

WBOY
WBOYOriginal
2016-05-16 16:07:581011browse

The examples in this article summarize the common control methods of jQuery operating forms. Share it with everyone for your reference. The details are as follows:

The following JS code lists common methods for jQuery to operate common controls on forms (including select, radiobox, checkbox). I believe there must be what you need

HTML code for radio operation

Radion
<input type="radio" name="rd" id="rd1" checked="checked" value="rd1" />
<input type="radio" name="rd" id="rd2" value="rd2" />
<input type="radio" name="rd" id="rd3" value="rd3" />

1. Get the selected value, three methods are available:

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();

2. Set the first Radio as the selected value:

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

or

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

Note:

attr("checked",'checked')= attr("checked", 'true')= attr("checked",true)

3. Set the last Radio as the selected value:

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

or

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

4. Set any radio as the selected value according to the index value:

$('input:radio').eq(索引值).attr('checked', 'true');

Index value=0,1,2....
or

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

5. Set Radio as the selected value according to the Value value

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

or

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

6. Delete the Radio whose Value is rd2

$("input:radio[value='rd2']").remove();

7. Which Radio to delete

$("input:radio").eq(索引值).remove();

Index value=0,1,2....
If you want to delete the 3rd Radio:

$("input:radio").eq(2).remove();

8. Traverse Radio

$('input:radio').each(function(index,domEle){
//写入代码
});

The following code demonstrates the common operation methods of DropDownList

DropDownList
<select id="sel">
<option value="1" selected="selected">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
<option value="5">e</option>
</select>

1. Get the Value of the selected item:

$('select#sel option:selected').val();

or

$('select#sel').find('option:selected').val();

Get the Text value of the selected item:

$('select#seloption:selected').text();

or

$('select#sel').find('option:selected').text();

2. Get the index value of the currently selected item:

$('select#sel').get(0).selectedIndex;

3. Get the maximum index value of the current option:

$('select#sel option:last').attr("index")

4. Get the length of DropdownList:

$('select#sel')[0].options.length;

or

$('select#sel').get(0).options.length;

5. Set the first option as the selected value:

$('select#sel option:first').attr('selected','true')

or

$('select#sel')[0].selectedIndex = 0;

6. Set the last option as the selected value:

$('select#sel option:last).attr('selected','true')

7. Set any option as the selected value based on the index value:

$('select#sel')[0].selectedIndex =索引值;

Index value=0,1,2....
8. Set the option with Value=4 as the selected value:

$('select#sel').attr('value','4');

or

$("select#sel option[value='4']").attr('selected', 'true');

9. Delete the option with Value=3:

$("select#sel option[value='3']").remove();

10. Which option should be deleted:

$(" select#sel option ").eq(索引值).remove();

Index value=0,1,2....
If you want to delete the 3rd Radio:

$(" select#sel option ").eq(2).remove();

11. Delete the first option:

$(" select#sel option ").eq(0).remove();

or

$("select#sel option:first").remove();

12. Delete the last option:

$("select#sel option:last").remove();

13. Delete dropdownlist:

$("select#sel").remove();

14. Add an option after select:

$("select#sel").append("<option value='6'>f</option>");

15. Add an option in front of select:

$("select#sel").prepend("<option value='0'>0</option>");

16. Traverse options:

$(' select#sel option ').each(function (index, domEle) {
//写入代码
});

The following code demonstrates jQuery’s common methods of operating checkboxes

CheckBox
<input type="checkbox" id="ck1" name="ck" vlaue="1" checked="checked" />
<input type="checkbox" id="ck2" name="ck" vlaue="2" />
<input type="checkbox" id="ck3" name="ck" vlaue="3" />
<input type="checkbox" id="ck4" name="ck" vlaue="4" />

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

$("input:checkbox:checked").val()

or

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

or

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

2. Get multiple checkbox selected items:

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

3. Set the first checkbox as the selected value:

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

or

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

4. Set the last checkbox as the selected value:

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

or

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

5. Set any checkbox as the selected value based on the index value:

$('input:checkbox).eq(索引值).attr('checked', 'true');

Index value=0,1,2....
or

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

6. Select multiple checkboxes:
Select the 1st and 2nd checkboxes at the same time:

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

7. Set the checkbox to the selected value based on the Value value:

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

8. Delete the checkbox with Value=1:

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

9. Which checkbox to delete:

$("input:checkbox").eq(索引值).remove();

Index value=0,1,2....
To delete the third checkbox:

$("input:checkbox").eq(2).remove();

10.遍历checkbox:

$('input:checkbox').each(function (index, domEle) {
//写入代码
});

11.全部选中

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

12.全部取消选择:

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

希望本文所述对大家的jQuery程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn