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

时间:2016-09-08 10:50:43

标签: php

我什么时候会更新我的学期,默认情况下会选中一个复选框来检查它是怎么可能的?

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

6 个答案:

答案 0 :(得分:3)

您可以使用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>

答案 1 :(得分:2)

如果您想选择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");
});

答案 2 :(得分:2)

请试试这个

<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>

答案 3 :(得分:0)

是的,有可能。您必须在selected

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

<强> DEMO

答案 4 :(得分:0)

<option value="1" <?= ($value=='1')?'selected':"" ?>>Semester 1</option>
<option value="2" <?= ($value=='2')?'selected':"" ?>>Semester 2</option>
<option value="3" <?= ($value=='3')?'selected':"" ?>>Semester 3</option>

您可以获取价值表单数据库或会话

答案 5 :(得分:0)

我们确实需要您的特定PHP代码来提供帮助,但是这个通用示例可能会告诉您在获得数据和期望值时如何执行此操作:

<?php
    //Your array of data
    $myData = array(
        '1' => 'Semester 1',
        '2' => 'Semester 2',
        '3' => 'Semester 3');

    //The value you want to be selected
    $myValue = "2";
?>

<select name="semester">
    <option value="">Select Semester</option>
        <?php
            foreach($myData as $key => $value){
                echo ('<option value="'.$key.'"' );
                if( $myValue == $key){
                    echo( ' selected="selected" ');
                }
                echo ('>'.$value.'</option');
                echo "\n";
            }
        ?>
</select>