1. 程式人生 > >struts2動態下拉框和複選框取值

struts2動態下拉框和複選框取值

下拉框取值

   action 程式碼:

//得到下拉框的list值,並傳入頁面

public String execute() throws Exception
    {
        ActionContext context=ActionContext.getContext();
        List<Test> lists = new ArrayList<Test>();
        for (int i = 0; i < 3; i++)
        {
            Test test = new Test();
            test.setCode("440000"+i);
            test.setName("廣東省"+i);
            lists.add(test);

       }
        context.getSession().put("lists", lists);
      
        return SUCCESS;
    }

//獲取下拉框選擇的值

public String gainSelect()
    {
        this.testCode = this.getTestCode();
        return null;
    }

public String getTestCode()
    {
        return testCode;
    }

    public void setTestCode(String testCode)
    {
        this.testCode = testCode;
    }

jsp頁面:

<s:form id="form" name="form" action="queryTestInfo!gainSelect.action" method="post">
  <s:select  name="testCode" list="#session.lists"
  listKey="code" listValue="name"/>
  //另一種list的寫法

list="%{#session.lists}"
  <s:submit></s:submit>
 </s:form>

 //修改頁面獲取下拉框選中的值

eg:

<s:select list="locationList" name="iMWUserInfo.attLocationCode"
id="iMWUserInfo.attLocationCode"
value="iMWUserInfo.attLocationCode" emptyOption="false" 
listKey="lastLevelCode" listValue="locationName">
</s:select>

複選框取值

action程式碼:

private String sourceParaList;

private String sourceParaListArray;

//得到複選框的值,傳入頁面

 public String gainSourcePara()
    {
        try
        {
            sourceParaList = this.getSourceParaBiz().findByIMSrcID(ecserpid, siappid);
        }
        catch (SQLException e)
        {
            log.error(e.toString());
            return ERROR;
        }
        return "success";
    }

//獲取頁面複選框的值

public String gainCheckBoxValue()

{

       sourceParaListArray = this.getSourceParaListArray();

       return "success";
}

  public int[] getSourceParaListArray()
    {
        return sourceParaListArray;
    }

    public void setSourceParaListArray(int[] sourceParaListArray)
    {
        this.sourceParaListArray = sourceParaListArray;
    }

public List<IMSourcePara> getSourceParaList()
    {
        return sourceParaList;
    }

    public void setSourceParaList(List<IMSourcePara> sourceParaList)
    {
        this.sourceParaList = sourceParaList;
    }

jsp頁面:
<s:form id="form" name="form" action="queryTestInfo!gainCheckBoxValue.action" method="post">
  <s:checkboxlist  name="sourceParaListArray" list="sourceParaList"
       listKey="paramseq"
       listValue="paramname">
     </s:checkboxlist>

<s:submit></s:submit>
 </s:form>

js 動態選中下拉框

for(var j=0;j<document.getElementById("Select"+rowID).options.length;j++)
{
if(document.getElementById("Select"+rowID).options[j].text==value[5])
index=j;
}
if(index>=0)
document.getElementById("Select"+rowID).options[index].selected=true;