Use the s: radio tag of Struts2 and add a change event with jquery

Source: Internet
Author: User

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:

With <s: radio name = "user. sex "list =" % {# {'1': 'male', '0': 'female} "theme =" simple "> </s: radio>
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
Copy codeThe Code is as follows:
Document. getElementsByName ('user. sex ') [0]. checked = true; // male is selected by default.

2. Add the value Attribute
Copy codeThe Code is as follows:
<S: radio name = "user. sex "list =" % {# {'1': 'male', '0 ': female} "value =" 1 "theme =" simple "> </s: radio>

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 to add a change event to the <s: radio/> label of Struts2.

When the <s: radio/> label of Struts2 is used, you want to add a change event to it. 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 set an id for this <s: radio/> as before, a number is automatically added to the generated radio group, for example:
Copy codeThe Code is 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"> Yes </label>

In this case, you can use jquery to implement it. It is very easy to add a cssClass = "filetype" attribute for this <s: radio/>, and a css class will be added for each radio, the code below is as follows:
Copy codeThe 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 ");
}
});
});

Related Article

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.