从jQuery下拉列表中访问变量 [英] Accessing a variable from inside a jQuery drop down list

查看:57
本文介绍了从jQuery下拉列表中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个下拉列表的jQuery下拉列表.第一个是汽车列表,第二个是汽车模型.一切都很好.现在,我想使用用户选择的选择来搜索数据库(MySQL)并在数据库中查找特定元素.这只是给我代码中的else,一旦给了我未定义的变量错误消息.

I have a jQuery dropdown list that has two dropdowns. The first one is a list of cars and the second one shows the car's models. This is all working fine. Now I would like to use the selection selected by the user to search a database (MySQL) and find a specific element within the database. This just gives me the else within my code and once it gave me a undefined variable error message.

如何对此进行编码,以便可以访问变量并显示搜索结果?这是我的代码:

How can I code this so that I can access the variables and display a search result? Here is my code:

这只是我使用的jQuery的一个示例-我实际上在使用汽车,但这显示了城市:

This is just an example of the jQuery I'm using - I'm actually using cars but this shows cities:

<select class="select" id="province" onchange="filterCity();">
  <option value="1">RM</option>
  <option value="2">FI</option>
</select>

 <select class="select" id="city" disabled>
  <option data-province="RM" value="1">ROMA</option>
  <option data-province="RM" value="2">ANGUILLARA SABAZIA</option>
  <option data-province="FI" value="3">FIRENZE</option>
  <option data-province="FI" value="4">PONTASSIEVE</option>
</select>

<span id="option-container" style="visibility: hidden; position:absolute;"></span>

<script>

    function filterCity(){
      var province = $("#province").find('option:selected').text(); // stores province
      $("#option-container").children().appendTo("#city"); // moves <option> contained in #option-container back to their <select>
      var toMove = $("#city").children("[data-province!='"+province+"']"); // selects city elements to move out
      toMove.appendTo("#option-container"); // moves city elements in #option-container
      $("#city").removeAttr("disabled"); // enables select
 };
</script>

这是我尝试访问搜索到的变量的代码(此代码回显了错误(否则)):我在第二个选择中使用了name ="car":

And here is the code where I'm trying to access the searched for variable (this code is echoing wrong (the else)): I used the name="car" in the second select:

if (isset($_POST['car'])) {
        $_SESSION['car'] = $_POST['car'];
    $car = $_SESSION['car'];
}
function find_tire(){
    if (isset($_POST['car'])) {
        $_SESSION['car'] = $_POST['car'];
    $car = $_SESSION['car'];
}

    global $db;

    if (isset($car)) {
    $query = $db->prepare("SELECT idtires FROM vehicle WHERE idcarmodel = $car");
 $query->execute();
    $tire = $query->fetchAll();
    var_dump($query);}
if (isset($tire)) {
            echo "<ul>";
        foreach ($tire as $name) {
                echo "<li id='tiresearch'>";
                echo "Tire Size is Available: " . $name    ['idtires'];
                echo "</li>";
            }echo "</ul>";
    } else {

            echo "Wrong";
        }
  }

推荐答案

我必须将所有select放入表单标签,使用其他页面进行搜索,然后将功能添加到搜索页面的底部,而不是对其进行访问在另一个页面上.

I had to put all the select inside form tags, use a different page for the search and add the function to the bottom of the search page instead of accessing it from a different page.

感谢所有发布答案并尝试帮助我的人.它正在工作(有一些错误),但我很高兴它终于可以工作.

Thank you to anyone who posted answers and tried to help me with this. It's working (with a few bugs) but I'm so happy it's finally working.

这篇关于从jQuery下拉列表中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆