Jquery adds option to the drop-down list

Source: Internet
Author: User
Tags tojson

Front-end:
 
Function load_allemployees (employee_type ){
Var url = "onLoadEmployee. do ";
Var p_data = "employee_type =" + employee_type;
DoAjaxSubmit (url, p_data, filSelect );
}
Function filSelect (data ){
$ ("# Left _"). empty ();
Var employeeArr = document. getElementById ('left _');
For (var I = 0; I <data. length; I ++)
{
Var op = document. createElement ("OPTION ");
Op. value = data [I]. user_id;
Op. innerHTML = data [I]. user_name;
EmployeeArr. appendChild (op );

}
}
Function doAjaxSubmit (p_url, p_data, p_callback ){
$. Ajax ({
Type: "POST ",
Url: p_url,
Data: p_data,
DataType: "json ",
Success: function (data ){
P_callback (data );
}
});
}

[Html]
Background:
Package com. aegon_cnooc.oa.admin.action;
 
Import java. io. PrintWriter;
Import java. util. List;
Import java. util. Map;
 
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Import net. sf. json. JSONArray;
Import net. sf. json. JSONSerializer;
Import net. sf. json. util. JSONUtils;
 
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionForward;
Import org. apache. struts. action. ActionMapping;
 
Import com. aegon_cnooc.framework.base.action.BaseAction;
Import com. aegon_cnooc.framework.log.Log;
Import com. aegon_cnooc.oa.admin.service.LoginService;
Import com. aegon_cnooc.util.LogPathUtil;
/**
*
* @ Despcrition:
* @ Author: liuxinghui
* @ Version: 1.0
* @ CreateDate: Jul 12,201 1
*/
Public class OnloadEmployeeAction extends BaseAction {
Private LoginService loginService;
Log logWrite = new Log ();
/**
* Click the single-choice button in the organizational structure definition to obtain the corresponding employees.
*/
Public ActionForward executeAction (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
LogWrite. writeLog (LogPathUtil. getPath (), "start OnloadEmployeeAction...", "info ");
String employeeType = request. getParameter ("employee_type ");
If ("all". equals (employeeType )){
// Employees all of company
List employeesAllOfCompany = loginService. getEmployeesAllOfCompany ();
LogWrite. writeLog (LogPathUtil. getPath (), "Load employeesAllOfCompany. size ():" + employeesAllOfCompany. size (), "info ");
JSONArray json = (JSONArray) JSONSerializer. toJSON (employeesAllOfCompany );
Response. setContentType ("application/json; charset = gbk ");
Response. setCharacterEncoding ("gbk ");
PrintWriter pw = response. getWriter ();
Pw. write (JSONUtils. valueToString (json ));
Pw. flush ();
} Else {
// Employees not belong to any groupp
List employeeNotBelongToAnyGroup = loginService. getEmployeesNotBelongToAnyGroup ();
LogWrite. writeLog (LogPathUtil. getPath (), "Load employeeNotBelongToAnyGroup. size ():" + employeeNotBelongToAnyGroup. size (), "info ");
JSONArray json = (JSONArray) JSONSerializer. toJSON (employeeNotBelongToAnyGroup );
Response. setContentType ("application/json; charset = gbk ");
Response. setCharacterEncoding ("gbk ");
PrintWriter pw = response. getWriter ();
Pw. write (JSONUtils. valueToString (json ));
Pw. flush ();
}
Return null;
}
Public void setLoginService (LoginService loginService ){
This. loginService = loginService;
}
Public static void main (String [] args ){
String testStr =
"{\" Employees \ ": [{\" user_id \ ": 841, \" user_name \ ": \" EnidChen \ "},{ \" user_id \": \ "1445 \", \ "user_name \": \ "JulieZhu \" },{ \ "user_id \": \ "1447 \", \ "user_name \": \ "JasonMa \"}]} ";

Www.2cto.com
System. out. println (testStr );
Map json = (Map) JSONSerializer. toJSON (testStr );
List employees = (List) json. get ("employees ");
System. out. println (employees. size ());
Map json2 = (Map) JSONSerializer. toJSON (employees. get (0 ));

System. out. println (json2.get ("user_id "));
System. out. println (json2.get ("user_name "));

}
}

From xinghui_liu's column

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.