When the s: radio tag of Struts2 is used, you want to add a change event to it. Because this tag automatically generates a radio group for the page and cannot be controlled as normal, you want to implement it using jquery.
Summary of the use of the s: radio tag in struts2
Problem: When this label is used, the default selected items are set. However, after the data is submitted and returned, the selected items cannot be displayed in a single region and are still the default options.
Through testing, the following conclusions are drawn:
To Example
When using the s: radio tag, if you want to select a tag by default, you can use either of the following methods:
1. Implemented through js Code
The Code is as follows:
Document. getElementsByName ('user. sex ') [0]. checked = true; // male is selected by default.
2. Add the value Attribute
The Code is as follows:
When setting the default option of a single refresh by using the value attribute, the default option is selected no matter when the status of the single refresh page is selected.
Therefore, if you want the data to be submitted to the backend, the single commit item is still the selected item and its default item cannot be set through the value attribute, you should set it through the first method.
In addition, the backend can correctly obtain the value of this ticket, but it is irrelevant to the value setting.
Use jquery Label add change event
Use Struts2 You want to add a change event to a tag. Because this tag automatically generates a radio group for the page, it cannot be controlled as usual. I spoke a lot on the Internet and did not find a satisfactory one. If you give this Set an id. A number is automatically added to the generated radio group, for example:
The Code is as follows:
No
Yes
In this case, jquery can be used for implementation, which is very simple. Add cssClass = "filetype" to add a css class for each radio. The following code is shown:
The Code is as follows:
$ (Function (){
$ (". Filetype"). change (function (){
Var val = $ ("input [name = 'file']: checked"). val (); // obtain the value of the selected radio
If (val = '1 '){
Alert ("yes ");
} Else {
Alert ("no ");
}
});
});