1

下面我有一段代码,它假设显示 2 个下拉菜单,一个用于建筑,另一个用于房间。发生的情况是,当用户从建筑物下拉菜单中选择建筑物时,使用 ajax/jquery 它将导航到room.php页面并列出属于所选建筑物的房间,并在房间下拉菜单中显示房间列表。这工作正常:

<script type="text/javascript">

    function getRooms() { 
    var building = jQuery("#buildingsDrop").val(); 
    jQuery('#roomsDrop').empty(); 
    jQuery('#roomsDrop').html('<option value="">Please Select</option>'); 
    jQuery.ajax({ 
          type: "post", 
          url:  "room.php", 
          data: { building:building }, 
          success: function(response){ 
              jQuery('#roomsDrop').append(response); 
          } 
        }); 


 }

</script>

...

<?php

 $sql = "SELECT DISTINCT Building FROM Room"; 

 $sqlstmt=$mysqli->prepare($sql);

 $sqlstmt->execute(); 

 $sqlstmt->bind_result($dbBuilding);

 $buildings = array(); // easier if you don't use generic names for data 

 $buildingHTML = "";  
 $buildingHTML .= '<select name="buildings" id="buildingsDrop" onchange="getRooms();">'.PHP_EOL; 
 $buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) 
 { 
     $building = $dbBuilding; 
     $buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;  
  } 

  $buildingHTML .= '</select>'; 

  $roomHTML = "";  
  $roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL; 
  $roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
  $roomHTML .= '</select>'; 

?>

房间.php:

$building = isset($_POST['building']) ? $_POST['building'] : '';

$sql = "SELECT Room FROM Room WHERE Building = ?";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("s",$building);
$sqlstmt->execute();
$sqlstmt->bind_result($dbRoom);

$roomHTML  = "";

while($sqlstmt->fetch()) {
    $roomHTML .= "<option value='".$dbRoom."'>" . $dbRoom . "</option>".PHP_EOL;
}
echo $roomHTML;

我遇到的问题是当用户选择评估时,假设在相关下拉菜单中显示评估的建筑和房间选项。但它并没有选择那些选项,它们保留在“请选择”选项上。为什么会这样,我怎样才能获得显示的选项?

下面是查看源代码:

    //Assessment drop down menu:

    <p><strong>Assessments:</strong> <select name="session" id="sessionsDrop">
    <option value="">Please Select</option>
    <option value='71' style='color: green'>AKXMB - 30-11-2012 - 10:00</option>
    </select> </p>   
    </form>

    //Building drop down menu:

    <select name="buildings" id="buildingsDrop" onchange="getRooms();">
    <option value="">Please Select</option>
    <option value='Canalside East'>Canalside East</option>
    <option value='Canalside West'>Canalside West</option>
    </select>


    //Room drop down menu (list of rooms displayed in room.php):

    <select name="rooms" id="roomsDrop">
    <option value="">Please Select</option>
    </select>

    //Retrieve assessment information 
    //(Below is where problem lies where it is not selecting building and room options in drop down menu)
    <script type="text/javascript">

$(document).ready( function(){

                var sessioninfo = [{"SessionId":71,"Building":"Canalside East","Room":"CE01\/04"},{"SessionId":84,"Building":"Canalside East","Room":"CE01\/04"}];

                $('#sessionsDrop').change( function(){

                    var sessionId = $(this).val();

                    if (sessionId !== '') {
                for (var i = 0, l = sessioninfo.length; i < l; i++)
                {
                        if (sessioninfo[i].SessionId == sessionId) { 

                var currentbuilding = $('#currentBuilding').val(sessioninfo[i].Building);
                var editbuilding = $('#BuildingsDrop').val(sessioninfo[i].Building);
                var currentroom = $('#currentRoom').val(sessioninfo[i].Room);
                var editroom = $('#RoomsDrop').val(sessioninfo[i].Room);
                var currentid = $('#currentId').val(sessioninfo[i].SessionId);
                var editid = $('#newId').val(sessioninfo[i].SessionId);

                        break;
                }
               }
             }

                });
            });
</script>

更新:

应用

  • 在应用程序中,从下拉菜单中选择“模块”并提交。

  • 它下面应该显示一些功能。在评估下拉菜单中选择任何评估。

  • 您可以在“当前评估详细信息”下方看到它在只读文本输入中显示建筑物和房间,以指示评估的当前建筑物和房间是什么。

  • 我希望在“新评估室”部分的下拉菜单中选择相同的建筑物和房间。您可以看到在“建筑物”下拉菜单中选择了建筑物,但在“房间”下拉菜单中未选择“房间”。

4

1 回答 1

1

除非这是复制/粘贴错误,否则您<script></script>在评估功能/脚本周围缺少标签 -

<script type="text/javascript">
$(document).ready( function(){

...
    });
</script>

编辑

你的问题是你id的情况是错误的 -

        ...
        var editbuilding = $('#BuildingsDrop').val(sessioninfo[i].Building);
        ...
        var editroom = $('#RoomsDrop').val(sessioninfo[i].Room);

改成 -

        ...
        var editbuilding = $('#buildingsDrop').val(sessioninfo[i].Building);
        ...
        var editroom = $('#roomsDrop').val(sessioninfo[i].Room);

另请参阅 -在 DOM 中节点 ID 是否区分大小写?

编辑 - 2只需在填充之前
添加​​,这样您就可以设置选定的。getRooms()var editroom#roomsDrop

var currentbuilding = $('#currentBuilding').val(sessioninfo[i].Building);
var editbuilding = $('#buildingsDrop').val(sessioninfo[i].Building);
var currentroom = $('#currentRoom').val(sessioninfo[i].Room);
getRooms();
var editroom = $('#roomsDrop').val(sessioninfo[i].Room);
var currentid = $('#currentId').val(sessioninfo[i].SessionId);
var editid = $('#newId').val(sessioninfo[i].SessionId);

编辑 - 3

默认情况下,$.ajax在浏览器中异步运行,所以问题是在你进入之前success: function(response){}function getRooms()已经移动到var editroom = $('#roomsDrop').val(sessioninfo[i].Room);,并且由于jQuery('#roomsDrop').append(response);没有附加选项值,roomsDrop所以没有什么可以选择的。这可以通过两种方式解决 -

(1)快速修复使用async: false

<script type="text/javascript">
    function getRooms() { 
    var building = jQuery("#buildingsDrop").val(); 
    jQuery('#roomsDrop').empty(); 
    jQuery('#roomsDrop').html('<option value="">Please Select</option>'); 
    jQuery.ajax({ 
          type: "post", 
          url:  "room.php", 
          data: { building:building },
          async: false, 
          success: function(response){ 
              jQuery('#roomsDrop').append(response); 
          } 
        }); 
 }
</script>

这使调用同步进行,因此在完成之前$.ajax不会继续进行。注意,在等待响应时会冻结浏览器,因此它可能会阻止任何其他操作。var editroom = $('#roomsDrop').val(sessioninfo[i].Room);success: function(response){}async: false

(2)使用回调函数——

<script type="text/javascript">
    function getRooms(callback) { 
    var building = jQuery("#buildingsDrop").val(); 
    jQuery('#roomsDrop').empty(); 
    jQuery('#roomsDrop').html('<option value="">Please Select</option>'); 
    jQuery.ajax({ 
          type: "post", 
          url:  "room.php", 
          data: { building:building },
          async: false, 
          success: function(response){ 
              jQuery('#roomsDrop').append(response);
              callback();
          } 
        }); 
 }
</script>    

getRooms(function(){
var editroom = $('#roomsDrop').val(sessioninfo[i].Room);});

此回调函数将在完成var editroom = $('#roomsDrop').val(sessioninfo[i].Room);后执行getRooms(),但将继续执行其余的操作,script而不用挂起浏览器

于 2013-01-01T17:57:27.883 回答