如何使用javascript更改选择列表jsp中的选项?

时间:2012-06-01 18:22:34

标签: javascript jsp struts

我有这个jsp页面,如果你单击一个单选按钮,一个子部分应该变为不可见,并且选择列表中的选项应该被更改。

<center>
  <span class="ReportSubsectionTitle">Report Type</span>
                                    <br />
 <html:radio  styleId="detail" property="reportOutputStyleID" onclick="doSummary();" value="1" />Pending Funding
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <html:radio  styleId="summary" property="reportOutputStyleID" onclick="doDetail();" value="2" />Previously Funded
 </center>
 <script>
 function doDetail(){   
    document.getElementById("narrowByDateRangeTop").checked = false;        
    document.getElementById("narrowByDateRangeBottom").checked = false;
    document.getElementById("narrowByDateRangeTop").disabled = true;    
    document.getElementById("narrowByDateRangeBottom").disabled = true;
    document.getElementById("dateRangeTypeID").disabled = true;
    document.getElementById("dateRangeLabel").disabled = true;
    document.getElementById("startDateStr").disabled = true;
    document.getElementById("startDateLabel").disabled = true;
    document.getElementById("startDateCalendarTrigger").disabled = true;
    document.getElementById("endDateStr").disabled = true;
    document.getElementById("endDateLabel").disabled = true;
    document.getElementById("endDateCalendarTrigger").disabled = true;
    document.getElementById("startDateTime").disabled = true;
    document.getElementById("endDateTime").disabled = true;

    document.getElementById("narrowByDateRangeTop").style.visibility = "hidden";    
    document.getElementById("narrowByDateRangeBottom").style.visibility = "hidden";
    document.getElementById("dateRangeTypeID").style.visibility = "hidden";
    document.getElementById("dateRangeLabel").style.visibility = "hidden";
    document.getElementById("startDateStr").style.visibility = "hidden";
    document.getElementById("startDateLabel").style.visibility = "hidden";
    document.getElementById("startDateCalendarTrigger").style.visibility = "hidden";
    document.getElementById("endDateStr").style.visibility = "hidden";
    document.getElementById("endDateLabel").style.visibility = "hidden";
    document.getElementById("endDateCalendarTrigger").style.visibility = "hidden";
    document.getElementById("startDateTime").style.visibility = "hidden";
    document.getElementById("endDateTime").style.visibility = "hidden";

    document.getElementById("DateSelection").style.visibility = "hidden";

 }
 function doSummary(){  

    document.getElementById("narrowByDateRangeTop").checked = true;     
    document.getElementById("narrowByDateRangeBottom").checked = false;
    document.getElementById("narrowByDateRangeTop").disabled = false;   
    document.getElementById("narrowByDateRangeBottom").disabled = false;
    document.getElementById("dateRangeTypeID").disabled = false;
    document.getElementById("dateRangeLabel").disabled = false;
    document.getElementById("startDateStr").disabled = false;
    document.getElementById("startDateLabel").disabled = false;
    document.getElementById("startDateCalendarTrigger").disabled = false;
    document.getElementById("endDateStr").disabled = false;
    document.getElementById("endDateLabel").disabled = false;
    document.getElementById("endDateCalendarTrigger").disabled = false;
    document.getElementById("startDateTime").disabled = false;
    document.getElementById("endDateTime").disabled = false;

    document.getElementById("narrowByDateRangeTop").style.visibility = "visible";   
    document.getElementById("narrowByDateRangeBottom").style.visibility = "visible";
    document.getElementById("dateRangeTypeID").style.visibility = "visible";
    document.getElementById("dateRangeLabel").style.visibility = "visible";
    document.getElementById("startDateStr").style.visibility = "visible";
    document.getElementById("startDateLabel").style.visibility = "visible";
    document.getElementById("startDateCalendarTrigger").style.visibility = "visible";
    document.getElementById("endDateStr").style.visibility = "visible";
    document.getElementById("endDateLabel").style.visibility = "visible";
    document.getElementById("endDateCalendarTrigger").style.visibility = "visible";
    document.getElementById("startDateTime").style.visibility = "visible";
    document.getElementById("endDateTime").style.visibility = "visible";
    document.getElementById("DateSelection").style.visibility = "visible";

 }
 </script>  

然后,我有一个标记为“日期选择”的小节,我想隐身:

 <tr>
    <td align="center" style="padding-bottom: 5px;">
        <span id = "DateSelection" class="ReportSubsectionTitle">Date Selection</span>
    </td>
    <td align="center" style="padding-bottom: 5px;">
        <span class="ReportSubsectionTitle">Save Generated Report?</span>
    </td>
</tr>
<tr>
    <td valign="top" nowrap="nowrap">
        <%@ include file="dateSelectionSection1.jsp" %>
    </td>
    <td valign="top" nowrap="nowrap" style="padding-left: 10px;">
        <%@ include file="savedReportNameField.jsp" %>
    </td>
</tr>

我有一个选择列表

  <span style="padding-left: 5px;">
                                    Date Type:
                                    <html:select property="dateTypeID">
                                        <html:options collection="dateTypes" property="id" labelProperty="name"/>
                                    </html:select>
                                </span>

我尝试在select中添加id,但是如果找不到tag,则会出错。我想更改下拉列表中的选择并锁定选择。

我尝试了document.getElementById(“dateTypeID”)。options [0] .selected = 1.但是,之后没有任何变化。

我是JSP新手,并不太清楚如何处理这个问题。

1 个答案:

答案 0 :(得分:2)

说这是你的选择陈述:

<select name="master" size="1">
  <option value="newsvalue">News</option>
  <option value="webmastervalue">Webmaster</option>
  <option value="techvalue">Tech</option>
</select>

在用户勾选单选按钮后调用的javascript函数中,首先从selectList中删除所有选项,如下所示:

document.myform.master.options.length=0;

然后你重新填充selectList做这样的事情:

document.myform.master.options[0]=new Option("Sports", "sportsvalue", true, false);  
document.myform.master.options[1]=new Option("Music", "musicvalue", false, false);
document.myform.master.options[2]=new Option("Movies", "moviesvalue", false, false);

“new Option()”函数的数据类型为:

new Option(text, value, defaultSelected, selected);

因此,JSP中的处理方式与HTML页面不同。希望你发现这个有用: - )