[Original] knockout -- add radio checked binding

Source: Internet
Author: User

Directly run the Code:

<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p><div data-bind="visible: wantsSpam">    Preferred flavor of spam:    <div><input type="radio" name="flavorGroup" value="cherry" data-bind="checked: spamFlavor" /> Cherry</div>    <div><input type="radio" name="flavorGroup" value="almond" data-bind="checked: spamFlavor" /> Almond</div>    <div><input type="radio" name="flavorGroup" value="msg" data-bind="checked: spamFlavor" /> Monosodium Glutamate</div></div> <script type="text/javascript">    var viewModel = {        wantsSpam: ko.observable(true),        spamFlavor: ko.observable("almond") // Initially selects only the Almond radio button    };         // ... then later ...    viewModel.spamFlavor("msg"); // Now only Monosodium Glutamate is checked</script>

This is a piece of code on the official website. It can be seen that there are three radio whose checked attributes are bound to spamflavor. The initial value is "Almond ", in this way, the default value is "Almond" radio is selected.

At first, I thought that the valid value of checked is true or false, and then I realized that the original valid value is the value attribute of radio.

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.