【问题标题】:conditional statement in jsp pagejsp页面中的条件语句
【发布时间】:2013-06-05 17:13:21
【问题描述】:

我有一个单选按钮列表来选择两个服务之一。

<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>

我需要为这两个选项显示不同的行。 When Service-Wise is selected then Service name should be displayed and when District-Wise is selected then District name should be displayed.例如在上面的代码中,我显示了服务名称,但实际上它应该只在选择服务明智选项时出现。我只停留在显示部分,这就是为什么我没有在代码中提到任何数据检索部分。我想在jsp页面本身做,非常感谢有人的帮助。

【问题讨论】:

  • 所以您想根据所选单选按钮在页面加载时显示此“服务名称”,还是在页面加载后动态执行此操作? (即用户更改单选按钮,新数据显示在 TD 标签中)如果页面加载后,那么您需要 JavaScript。否则@DwB 已经回答了你的问题。
  • 我想在页面加载后执行此操作,当用户更改单选按钮时,显示会相应更改

标签: jsp


【解决方案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/

【讨论】:

    【解决方案2】:

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

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

    显示服务的东西 当> 展示区的东西 否则> 选择>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多