Use of Struts2 S:radio tags and add a change event with jquery _jquery

Source: Internet
Author: User
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");
}
});
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.