下拉列表中的默认值

时间:2019-12-18 12:40:24

标签: javascript php html ajax drop-down-menu

我必须在下拉列表中将“选择选项”显示为禁用选项。下拉列表从另一个页面显示。

page1.php

<div class="col-md-4">
            <select  class="form-control" name="task" id="task" required>
             <option selected="selected" value="">Choose Task</option>
             <option selected="selected" value="task1">Task 1</option>
             <option selected="selected" value="task2">Task 2</option>
            </select>
           </div>
        <div class="col-md-4">
          <select  class="form-control" name="subtask" id="subtask" required>
             <option selected="selected" value="">Choose SubTask</option>
           </select>
         </div>

javascript部分:

$("#task").change(function()
{
    var id=$(this).val();
    $.ajax
    ({
        type: "POST",
        url: "query.php",
        data:{id: id,
            task: 1,
        },
        cache: false,
        success: function(html)
        {
            $("#subtask").html(html);
        } 
    });
});

query.php

if($_POST['task'])
{
    $id1=$_POST['id'];
    $sql2="SELECT * FROM sub WHERE task_int='$id1'";
    $result2=$conn->query($sql2);
    echo '<option disabled selected value> Choose option</option>';
    while($row2=$result2->fetch_assoc())
    {
        $id=$row2['sub_int'];
        $data=$row2['sub_name'];
        echo '<option value="'.$id.'">'.$data.'</option>';
    }
}   

我已经尝试了上面的代码,但是在从数据库检索到的其他数据之上,它没有显示“ Choose Option”。

2 个答案:

答案 0 :(得分:0)

<div class="col-md-4"><select class="form-control" name="task" id="task" required="">
        <option selected="selected" value="0" disabled="disabled">Choose option</option>
        <option value="">Choose Task</option>
        <option value="task1">Task 1</option>
        <option value="task2">Task 2</option>
    </select></div>
<div class="col-md-4"><select class="form-control" name="subtask" id="subtask" required="">
        <option selected="selected" value="" disabled="disabled">Choose option</option>
        <option value="">Choose SubTask</option>
    </select></div>`enter code here`

答案 1 :(得分:0)

您必须将php代码更改为此

if($_POST['task'])
{
    $id1=$_POST['id'];
    $sql2="SELECT * FROM sub WHERE task_int='$id1'";
    $result2=$conn->query($sql2);
    echo '<select  class="form-control" name="subtask" id="subtask" required>';
     echo '<option value>Choose SubTask</option>';
     echo '<option disabled  selected value> Choose option</option>';
    while($row2=$result2->fetch_assoc())
    {
        $id=$row2['sub_int'];
        $data=$row2['sub_name'];
        echo '<option value="'.$id.'">'.$data.'</option>';
    }
   echo '</select>'
}