How to select a radio button by default?

This question already has an answer here:

  • Assign an initial value to radio button as checked 7 answers

  • XHTML solution:

    <input type="radio" name="imgsel" value="" checked="checked" />
    

    Please note, that the actual value of checked attribute does not actually matter; it's just a convention to assign "checked" . Most importantly, strings like "true" or "false" don't have any special meaning.

    If you don't aim for XHTML conformance, you can simplify the code to:

    <input type="radio" name="imgsel" value="" checked>
    

    Use the checked attribute.

    <input type="radio" name="imgsel"  value="" checked /> 
    

    or

    <input type="radio" name="imgsel"  value="" checked="checked" /> 
    

    This doesn't exactly answer the question but for anyone using AngularJS trying to achieve this, the answer is slightly different. And actually the normal answer won't work (at least it didn't for me).

    Your html will look pretty similar to the normal radio button:

    <input type='radio' name='group' ng-model='mValue' value='first' />First
    <input type='radio' name='group' ng-model='mValue' value='second' /> Second
    

    In your controller you'll have declared the mValue that is associated with the radio buttons. To have one of these radio buttons preselected, assign the $scope variable associated with the group to the desired input's value:

    $scope.mValue="second"
    

    This makes the "second" radio button selected on loading the page.

    链接地址: http://www.djcxy.com/p/87820.html

    上一篇: x:隐藏增加了一个垂直滚动条

    下一篇: 如何选择默认的单选按钮?