Use AJAX to load a data dictionary, generate a Select

Source: Internet
Author: User

//uses Ajax to load the data dictionary, generating select//parameter 1: Data dictionary type (DICT_TYPE_CODE)//Parameter 2: Put down the selected tag id//parameter 3: When generating the drop-down, the Name property value of the Select tag//Parameter 4: When a echo is required, select which Optionfunction loadselect (Typecode,positionid, Selectname,selectedid) {//1 Create a Select object, specify the Name property var $select = $ ("<select name=" +selectname+ "></select>") //2 add hint Option $select.append ($ ("<option value= >---Select---</option>");//3 use jquery's Ajax method to access background action$. Post ("${pagecontext.request.contextpath}/basedictaction", {Dict_type_code:typecode}, function (data) {//Traversal//4 Returns a JSON array object that iterates over its $.each (data, function (I, JSON) {//each traversal creates an option object var $option = $ ("<option value= '" +json[' dict_id    ']+ ' > ' +json[' dict_item_name "]+" </option> "); if (json[' dict_id ' = = Selectedid) {//Determine if it needs to be echoed, if necessary to make it selected $option.attr ("Selected", "Selected");}   And added to the Select Object $select.append ($option);  }); }, "JSON");//5 the assembled select object into the page at the specified location $ ("#" +positionid). Append ($select);} 


function Loaduserroleselect (typeurl,positionid,selectname,sid,selectedid) {

Create a Select
var $select = $ ("<select name=" +selectname+ "id=" +sid+ "></select>");
$select. Append ($ ("<option value=" >---Please select---</option> "));

$.ajax ({
"Async": true,
"Type": "Post",
"url": Typeurl,
"Success": function (data) {
$.each (Data,function (I,json) {
var $option = $ ("<option value=" "+json.id+" > "+json.rolename+" </option> ");
$select. Append ($option);
if (json[' userrole '] = = Selectedid) {
Determine if it needs to be echoed if necessary to make it selected
$option. attr ("Selected", "Selected");
}
})
},
"DataType": "JSON"
});
$ ("#" +positionid). Append ($select);
}



<script type= "Text/javascript" >
$ (function () {
Loaduserroleselect (
"${pagecontext.request.contextpath}/userrolelist.do",
"Selectbd", "Userrole", "Userrole", "${user.userrole}");
});
</script>

  

Use AJAX to load a data dictionary, generate a Select

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.