jsp_strusts2 下拉框的取值赋值 和选中[从数据库] 流程

<tr>
<td align="right">
所属广告类别
</td>
<td colspan='1'>
<select id='paid' name='paid' style='width: 150px'></select>
 </td>
</tr>
 
<script type='text/javascript'>
function AddSelectOption( selectElement, optionText, optionValue ,optionSelect){
var oOption = document.createElement('OPTION') ;
oOption.text	= optionText ;
oOption.value	= optionValue ;
oOption.selected = optionSelect;
selectElement.options.add(oOption) ;
if(optionSelect=='selected')
selectElement.value=optionText;
return oOption ;}
${selectOptions}
</script>
 
public class Constants {
	public final static String SELECTOPTIONS = "selectOptions";

 }
 
	// 获取所属广告的类别 
		Pageadvert pa = new Pageadvert();
		List paList = pageadvertManager.getList(null);
		if (paList.size() != 0) {
			List<String> selectList = new ArrayList<String>();
			for (int i = 0; i < paList.size(); i++) {
				pa = (Pageadvert) paList.get(i);
				String id = pa.getId();
				String name = pa.getName();
				String val = name + "," + id;
				selectList.add(val);
			}
			String messages = (String) getRequest().getAttribute(
					Constants.SELECTOPTIONS);
			if (messages == null) {
				messages = "";
			}
			String obj = JsUtils.addSelectOptions("paid", null, selectList);
			messages += obj;
			saveRequest(Constants.SELECTOPTIONS, messages);
		}
 
	/**
	 * 为select下拉框赋值 BYList
	 * @param filedName 字段名称 如 paid
	 * @param list  存放String 格式 list.add("生活广告,life");list.add("服务广告,service");
	 * @return
	 */
	public static  String addSelectOptions(String filedName,String selectedValue, List<String> list){
		StringBuffer sb = new StringBuffer();
		for (String str : list) {
			String[] vals = str.split(",");
			String selected = "";
			sb.append(addSelectOption(filedName,vals[0],vals[1],selected));
		}
		if(StringUtils.isNotEmpty(selectedValue) )
		{
			sb.append(setSelectedOption(selectedValue,filedName));
		}
		return sb.toString();
	}
	/**
	 * 为select下拉框赋值
	 * @param filedName 字段名称
	 * @param lable     显示名称
	 * @param value     字段值
	 * @param selected     是否被选择
	 * @return
	 */
	public static  String addSelectOption(String filedName,String lable,String value,String selected){
		return "AddSelectOption(document.all."+filedName+",'"+lable+"','"+value+"','"+selected+"');\n";
	}

	/**
	 * 设置下拉框选择的值
	 * @param value         字段值
	 * @param filedName     字段名称
	 * @return
	 */
	public static  String setSelectedOption(String value,String filedName){
		return "SetOptionSelected('"+value+"',document.all."+filedName+");\n";
	}

你可能感兴趣的:(JavaScript,select,strusts2)