我想要一个复选框被默认选中,

这个问题在这里已经有了答案:

  • 我如何设置HTML <select>元素的默认值? 18个答案

  • 请试试这个

    <select name="semester">
                <option value="">Select Semester</option>
                <option value="1" <?php if($data['semester']==1){echo 'selected';}?>>Semester 1</option>
                <option value="2" <?php if($data['semester']==2){echo 'selected';}?>>Semester 2</option>
                <option value="3" <?php if($data['semester']==3){echo 'selected';}?>>Semester 3</option>
    </select>
    

    您可以使用selected属性。

    <select name="semester">
         <option value="">Select Semester</option>
         <option value="1" selected="selected">Semester 1</option>
         <option value="2">Semester 2</option>
         <option value="3">Semester 3</option>
    </select>
    

    如果你想选择第二semester 2所以在这里你去:

    HTML:

    <select name="semester">
      <option value="">Select Semester</option>
      <option value="1">Semester 1</option>
      <option value="2" selected>Semester 2</option>
      <option value="3">Semester 3</option>
    </select>
    

    jQuery的:

    $("select option").each(function(){
      if ($(this).text() == "Semester 2")
        $(this).attr("selected","selected");
    });
    
    链接地址: http://www.djcxy.com/p/88073.html

    上一篇: I want a checkbox to be checked by default,

    下一篇: How to set default value for HTML select?