如何使用javascript更改选中的值

时间:2015-07-17 18:00:21

标签: javascript

我正在尝试更改默认选中的单选按钮,我无法访问html这样做,但我可以使用javascript。这是html

<div class="row" id="courseSearchAvailability">
        <div class="col-xs-12">
          <fieldset class="accessibility">
            <legend>
              Filter By Course Availability
            </legend>

         <div class="radio">
              <label> 
                <input type="radio" name="courseSearch.filterString" value="availforreg" id="searchAvailable"> 
                Search scheduled courses 
              </label>
            </div>
             <div class="radio">
              <label> 
                <input type="radio" name="courseSearch.filterString" value="all" checked="checked" id="searchAll"> 
                Search all courses 
              </label>
            </div>
          </fieldset>
        </div>
      </div>

我想使searchAvailable成为默认值,这里是我正在使用的javascript,它似乎不起作用。我是新手,所以请大声笑。

var sel = document.getElementById('searchAvailable');
sel.searchALL.removeAttribute('checked');
sel.searchAvailable.setAttribute('checked', 'checked');
document.forms[0].reset();

1 个答案:

答案 0 :(得分:3)

这两行应该足够了:

&#13;
&#13;
var sel = document.getElementById('searchAvailable');
sel.checked = true;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" id="courseSearchAvailability">
    <div class="col-xs-12">
        <fieldset class="accessibility">
            <legend>Filter By Course Availability</legend>
            <div class="radio">
                <label>
                    <input type="radio" name="courseSearch.filterString" value="availforreg" id="searchAvailable">Search scheduled courses</label>
            </div>
            <div class="radio">
                <label>
                    <input type="radio" name="courseSearch.filterString" value="all" checked="checked" id="searchAll">Search all courses</label>
            </div>
        </fieldset>
    </div>
</div>
&#13;
&#13;
&#13;