jsp页面中的条件语句

时间:2013-06-05 17:13:22

标签: jsp

我有一个单选按钮列表,可以选择两种服务之一。

<table align="center" width="100%">
    <tr>
        <td colspan="2">
            <input type="radio" name="selectedValue" value="serviceWise">
            Service-Wise
            &nbsp;&nbsp;&nbsp;
            <input type="radio" name="selectedValue" value="districtWise">
            District-Wise
        </td>
    </tr>
</table>

<table>
    <tr>
        <td>
        Service Name
        </td>
    </tr>
</table>

我需要为这两个选项显示不同的行。选择Service-Wise时,应显示Service name,当选择District-Wise时,应显示District name。例如在上面的代码中,我显示了服务名称,但只有在选择了Service-Wise选项时才会出现。我只被困在显示部分,这就是为什么我没有在代码中提到任何数据反向部分。我想在jsp页面本身做这件事,非常感谢别人的帮助。

2 个答案:

答案 0 :(得分:1)

更新:我忘记将#放在结果ID前面。现在它有效。

听起来你需要一些JavaScript,除非你打算在选择单选按钮时重新加载页面。

我正在使用jQuery来简化您的需求。在JSP页面的标记中添加以下内容。它将链接到jQuery javascript文件。它还会在名为“selectedValue”的单选按钮上添加一个更改侦听器,允许您动态更改表格单元格中的值。

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("input[name='selectedValue']").change(function() {
        if ($("input[name='selectedValue']:checked").val() == 'serviceWise')
            $("#result").text("Service Name");
        else if ($("input[name='selectedValue']:checked").val() == 'districtWise')
            $("#result").text("something else");
    });
});
</script>

还要更新您的表格,如下所示。我添加了id属性以便轻松引用单元格。

<table>
    <tr>
        <td id="result">
        Service Name
        </td>
    </tr>
</table>

我使用了这个答案(以及它的一些代码),因此您可能需要查看它:jQuery .change() on Radio Button

jsfiddle:http://jsfiddle.net/mikeyfreake/UUbxa/

答案 1 :(得分:0)

查看JSTL(特别是选择标记)。

这是一个例子(在测试细节和“显示元素”上松散):

    <choose>
       <when test="service is selected">
          show service stuff
       </when>
       <otherwise>
          show district stuff
       </otherwise>
    </choose>
相关问题