Summary of the use of S:radio tags in struts2
Problem encountered: When the label is used, the default selection is set, but when the submit data returns, the radio box cannot display the previously selected item, still the default option
The test draws the following conclusions:
Take <s:radio name= "User.sex" list= "%{#{' 1 ': ' Male ', ' 0 ': ' Female '}" theme= "simple" ></s:radio> for example
When using the S:radio label, if you want the label to be selected by default, there are two ways
1, through the JS code to achieve
Copy Code code as follows:
Document.getelementsbyname (' User.sex ') [0].checked=true;//Default selected Male
2, by adding the Value property
Copy Code code as follows:
<s:radio name= "User.sex" list= "%{#{' 1 ': ' Male ', ' 0 ': ' Female '}" value= "1" theme= "simple" ></s:radio>
When you set the default option for a radio box by using the Value property, the default item is selected whenever the page is refreshed and the state of the radio box is checked
Therefore, if you want the data to be submitted back to the background, the radio box is still the previously selected item, and you cannot set its default item through the Value property, you should set it by using the first method
In addition, the background can get the value of the radio box correctly, regardless of the setting of value
Add a Change event to the Struts2 <s:radio/> tag with jquery
You want to add a change event to the Struts2 <s:radio/> tab, because this tab automatically generates a radio group for the page, and it can't be controlled as usual. Said a lot of online, also did not find a satisfactory. If you set an ID for this <s:radio/> as before, you will automatically add a number in the generated radio group, such as:
Copy Code code as follows:
<input type= "Radio" name= "File" id= "Upfile_file0" value= "0" ><label for= "Upfile_file0" > No </label>
<input type= "Radio" name= "File" id= "Upfile_file1" checked= "Checked" value= "1" class= "filetype" ><label "for=" Upfile_file1 "> is </label>
This can be achieved by using jquery, very simply, to add a property to this <s:radio/> cssclass= "filetype", you will add a CSS class for each radio, the above can be seen, then the following code is as follows:
Copy Code code as follows:
$ (function () {
$ (". FileType"). Change (function () {
var val = $ ("input[name= ' file ']:checked"). Val ();//Get the value of the selected radio
if (val== ' 1 ') {
Alert ("Yes");
}else{
Alert ("No");
}
});
});