jQuery 来自 JSP scriptlet 变量的选中单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17265383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:43:12  来源:igfitidea点击:

checked radio button from JSP scriptlet variable

javascriptjqueryjspradio-buttonscriptlet

提问by AzAh

This is going to be a stupid question but i need to ask it. PS: this is an old practice but i need it this way. In scriptlet I have a variable status.

这将是一个愚蠢的问题,但我需要问它。PS:这是一种古老的做法,但我需要这种方式。在 scriptlet 中,我有一个变量status

 <%
   String status=bean.getStatus();
   //Status value is coming from database which is either "1" or "0"(String type)
 %>

In a form I have radio button.

在表单中,我有单选按钮。

<tr>
<td> Status :</td>
<td>Active
            <input type="radio" name="status" checked="checked" id="statusAct" value="1" />
    Inactive
           <input type="radio" name="status" id="statusInac" value="0"/>
</tr>

If status==1I want radio button with id="statusAct"to be selected and if status==0then radio with id="statusInac"to be selected.

如果status==1我想要选择单选按钮,然后id="statusAct"选择单选按钮。status==0id="statusInac"

I can use javascript or Jquery but I don't know how to put Java variable inside javascript function.

我可以使用 javascript 或 Jquery,但我不知道如何将 Java 变量放入 javascript 函数中。

回答by Ohgodwhy

You can simply do

你可以简单地做

<input type="radio" name="status" <% ${bean.getStatus() == '1' ? 'checked="checked"' : '' } %> id="statusAct" value="1" />

and

<input type="radio" name="status" <% ${bean.getStatus() == '0' ? 'checked="checked"' : '' } %> id="statusInac" value="1" />

回答by Thorn

Either use scriptlets to do conditional logic and write checked ="checked" where you need to. In the scriptlets you can do out. println to write to the page (output stream). You could also use jstl for condition tags c:if

要么使用 scriptlet 来做条件逻辑,并在需要的地方写 checked ="checked" 。在scriptlets中你可以做到。println 写入页面(输出流)。您还可以将 jstl 用于条件标签 c:if