Home > Web Front-end > JS Tutorial > Summary of operations on server controls DropdownList, RadioButtonList, CheckboxList in JQuery_jquery

Summary of operations on server controls DropdownList, RadioButtonList, CheckboxList in JQuery_jquery

WBOY
Release: 2016-05-16 18:05:20
Original
788 people have browsed it

1: DropDownList
------------------------------------------------- -----------------------------------------------
When using JQuery to perform traversal operations,
$("input").each(function(i) {
......
}
When the type of the operation object is dropdownlist: (Note: The type of DropDownList in Firefox is "select-one")
Get the selected value: $(this).val(); (If it is not a traversal operation, $(this) is replaced with $( '#The Id of the control') )
Get the selected text: $(this).find("option:selected").text(); or $("#The name of the control option:selected").text( );
Get the selected index: $(this).get(0).selectedIndex;
2: RadioButtonList
------------------- -------------------------------------------------- -----------------------
If the page has only one RadioButtonList, you can directly use $("input[type='radio']:checked") .val() to get the selected value
If the page has 2 or more RadioButtonLists:
Step 1: Get the Id of the RadioButtonList control, set var objId=controlId;
Second Step: Get the name of the control, set var radioName = $("input[id^='" objId "']").eq(0).attr('name');
Step 3: Get the value
Get the selected value: $("input[name='" radioName "']:checked").val());
Get the selected text: $("input[name='" radioName "']:checked label").text());

Three: CheckBoxList
----------------------- -------------------------------------------------- ------------------
There is a method to determine whether there is a selection, objId is the Id of CheckBoxList
Currently it is not possible to directly obtain the value of the server control CheckBoxList using js. , can only be achieved through some small tricks, such as adding additional attributes
In the code, selectedText is to get the selected text value, and selectedValue is to get the selected value

Copy code The code is as follows:

function hasCheckedByCheckbox(objId) {
var checkedCount = 0;
$("input[id^=' " objId "']").each(function() {
// var checkName = $(this).attr('name');
// var selectedText = $("input[name=' " checkName "']:checked label").text();
// var selectedValue = $(this).parent('span').attr('alt'); //Use hack to implement js To get the selected value of checkboxList, you need to add an additional attribute in if ($(this).attr('checked')) {
checkedCount;
}
});
return checkedCount > 0;
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template