xml地图|网站地图|网站标签 [设为首页] [加入收藏]

美高梅游戏网站

当前位置:美高梅游戏网站 > 美高梅游戏网站 > jquery为下拉框添加option

jquery为下拉框添加option

来源:http://www.gd-chuangmei.com 作者:美高梅游戏网站 时间:2019-09-04 04:25

前台: 
 
    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); 
            } 
        }); 
    } 

js获取select选中的值:

[html]
后台: 
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, 2011 
 */ 
public class OnloadEmployeeAction extends BaseAction{ 
    private LoginService loginService; 
    Log logWrite=new Log(); 
/** 
 * 组织架构定义中点击单选按钮获得相应的员工 
 */ 
    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  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  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")); 
         
    } 

var sel=document.getElementById("select1");
var index = sel.selectedIndex; // 选中索引
albumid= sel.options[index].value;//要的值

摘自 xinghui_liu的专栏

jQuery获取下拉框选中的option:

本文由美高梅游戏网站发布于美高梅游戏网站,转载请注明出处:jquery为下拉框添加option

关键词: