JS implements the radio button echo when the page effect appears, but the value of the selected radio box is empty

Source: Internet
Author: User

  Recently done a lot of front-end page work, encountered a feeling very headache in the matter here to remember:

Often use JS to echo the radio box, but clearly from the page effect, the Radio box has been selected, but it can not trigger the Change event, the value of the time to take a certain method can not take the selection of the selected box value (single box to take the value of a number of methods, I mean some can be taken, some methods do not get).

Think for a long time, a set of seemingly complex but very practical JS echo method. experimented with a variety of front-end frameworks and browsers. Let's say we need to echo the single box with name Gender,value 1:

$ ("[name= ' Gender '][value=1]"). attr ("Checked", "checked"). Prop ("checked", true). Trigger ("change");

Attr and prop can guarantee that any method that gets the value of the selected radio box can be obtained, and the trigger method manually triggers the change event for the radio box. If the event of your radio box is another event, simply change the change to the appropriate event.

JS implements the radio button echo when the page effect appears, but the value of the selected radio box is empty

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.