【问题标题】:select option tag does not change using ajax/php call?选择选项标签不会使用 ajax/php 调用更改?
【发布时间】:2014-02-28 20:39:58
【问题描述】:

下面的代码尝试通过 Ajax 调用来更改 Select 选项标签,该调用连接到服务器上的 PHP 并检索数据。但是,如果我将响应文本设置为跨度标记,则下面的代码可以正常工作,但它不适用于 Select 选项标记?

<?php

$q = intval($_GET['q']);

$con=mysqli_connect("localhost","rot","ro","abc","3306");

/

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$result = mysqli_query($con,"SELECT DISTINCT(UniqueBusId) FROM cfv_busstopnames WHERE busnumber = ".$q." ");


if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}


while($row = mysqli_fetch_array($result))
  {
   echo "<option>" . $row['UniqueBusId'] . "</option>" ;

  }

mysqli_close($con);


?>

.

<script>
  function showUser(str)
  {

  if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    } 
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
      document.getElementById("select-choice-direction").innerHTML=xmlhttp.responseText;
      }
    }
  xmlhttp.open("GET","getdirection.php?q="+str.value,true);
  xmlhttp.send();
  }


</script>
<Body>
  <label for="select-choice-direction">Direction</label>
  <select name="select-choice-direction" id="select-choice-direction" data-native-menu="false" ">
    <option> Direction heading towards?</option>
    <option value="X">X</option>
    <option value="Y">Y</option>
    <!-- etc. -->
  </select>
</Body>

【问题讨论】:

  • 你为什么把它标记为 jQuery?你是如何触发你的函数的?

标签: php ajax jquery-mobile


【解决方案1】:

代替

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
       document.getElementById("select-choice-direction").innerHTML=xmlhttp.responseText;
    }
}

试试

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
        $('#select-choice-direction').html(xmlhttp.responseText).selectmenu( "refresh");
     }
}

这告诉 jQuery Mobile 刷新选择菜单小部件 (http://api.jquerymobile.com/selectmenu/#method-refresh)

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2015-03-04
    • 2018-08-25
    • 1970-01-01
    • 2020-02-19
    • 2017-07-15
    • 2016-08-15
    相关资源
    最近更新 更多