Java 使用弹簧的表单,更改单选按钮的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20222220/
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-13 00:24:04  来源:igfitidea点击:

Form using spring, change value of radio button

javajqueryspringspring-mvcspring-form

提问by user2785929

I have bean like this:

我有这样的豆子:

class StudentBean
{
    ...
    private String gender = "Male";
    ...
    // setters and getters here
}

The Spring form is something like this:

Spring 的形式是这样的:

<!-- Gender -->
<form:form action="url" method="POST" modelAttribute="studentBean">
     <form:radiobutton path="gender" id="male" value="Male" required="required" style="float:left;" checked="checked"/> 
     <form:radiobutton path="gender" id="female" value="Female" required="required" style="float:left;"/>
...
</form:form>

The gender radio button by default is set to Male, I want to change it using jQuery.

默认情况下,性别单选按钮设置为Male,我想使用 jQuery 更改它。

What I tried so far is:

到目前为止我尝试过的是:

$('.gender[value="'+ beanList[index].myGender +'"]').prop('checked', true);

$('.gender[value="'+ beanList[index].myGender +'"]').prop('checked', true);

it also the same with

它也与

$('.gender[value="Female"]').prop('checked', true);

$('.gender[value="Female"]').prop('checked', true);

But doesn't change the radio button. What am I doing wrong?

但不会更改单选按钮。我究竟做错了什么?

采纳答案by Michael Ford

What version of JQuery are you using and in what browser? FYI - You question appears to really be isolated to JQuery and what has already been rendered to the browser, so you may want to take off the spring related tags.

您使用的是什么版本的 JQuery 以及在什么浏览器中?仅供参考 - 您的问题似乎真的与 JQuery 以及已经呈现给浏览器的内容无关,因此您可能想要取消与 spring 相关的标签。

try making the change like this: $("input[value='Female']").prop('checked',true);

尝试进行如下更改: $("input[value='Female']").prop('checked',true);

<html>
<head>
<script src="jquery-1.9.1.js"></script>
</head>

<body>

<form>
  <input type="radio" path="gender" id="Male" name="sex" value="Male" checked="checked" />Male
  <br />
  <input type="radio" path="gender" id="Female" name="sex" value="Female" />Female
  <div id="test">my test</div>
</form>

<script>
alert("hello");
$("input[value='Female']").prop('checked',true);
alert("bye");
</script>

</body>
</html>

回答by GriffeyDog

genderisn't a class, but you're using a jQuery class selector.

gender不是类,但您使用的是 jQuery 类选择器。

Try this instead:

试试这个:

$("input[name='gender'][value='Female']").prop('checked', true);

回答by Joe Cullinan

You're selecting things with the gender class(the . prefix is class), whereas gender is an ID in your HTML. You could either change the selector to target the ID gender

您正在选择具有性别类的内容(. 前缀是类),而性别是 HTML 中的 ID。您可以更改选择器以定位 ID 性别

($('#gender[value="Female"]').prop('checked', true);)

or you could add classes to your HTML like this:

或者您可以像这样向 HTML 添加类:

<!-- Gender -->
<form:form action="url" method="POST" modelAttribute="studentBean">
<form:radiobutton path="gender" class="male" value="Male" required="required" style="float:left;" checked="checked"/> 
<form:radiobutton path="gender" class="female" value="Female" required="required" style="float:left;"/>
...
</form:form>

The correct solution depends on how many times an element will appear on the page. Multiple elements can have the same class, but only one element on a page can have any ID. If you're going to have multiple copies of this form, and therefore multiple gender radio buttons, then change the IDs to classes. If this form will only appear once then IDs are okay.

正确的解决方案取决于元素在页面上出现的次数。多个元素可以具有相同的类,但页面上只有一个元素可以具有任何 ID。如果您要拥有此表单的多个副本,因此需要多个性别单选按钮,请将 ID 更改为类。如果此表单只会出现一次,那么 ID 就可以了。