简体   繁体   English

级联选择框-jQuery

[英]Cascading Select Boxes - Jquery

Having a problem with dynamically populated select boxes using jQuery. 使用jQuery动态填充选择框时遇到问题。

Basically, my code calls a php script which populates the select box (utilityMainChart). 基本上,我的代码调用了一个php脚本,该脚本填充了选择框(utilityMainChart)。 Let's say it brings back two options for the the select box utilityMainChart, I want the next select box (parameterMainChart) to be populated with the options for whichever option is first in the utilityMainChart select box. 假设它为选择框实用程序MainChart带来了两个选项,我希望在下一个选择框(parameterMainChart)中填充实用程序MainChart选择框中第一个选项的选项。

As it is right now, the select boxes get populate properly initially, but the line var building = $('#utilityMainChart').val(); 现在,选择框最初会正确填充,但是var building = $('#utilityMainChart')。val(); never returns the value of the select box. 从不返回选择框的值。 This means that any change to a select box result in the ones below it becoming blank, since no value is picked up. 这意味着对选择框的任何更改都将导致选择框下方的内容变为空白,因为未选择任何值。

My code for the javascript is shown below: 我的javascript代码如下所示:

var data = "1" + "_" + building + "_";
var url = "/charts/data/utilityManagerMenuPopulate.php?data=" + data;
$('#utilityMainChart').load(url);

var building = $('#utilityMainChart').val();
var data = "2" + "_" + building + "_" + utility;
var url = "/charts/data/utilityManagerMenuPopulate.php?data=" + data;
$('#parameterMainChart').load(url);

My php code returns data in the form: 我的PHP代码以以下形式返回数据:

<select id="utilityMainChart" name="utilityMainChart">
    <option selected value=1>Electricity</option>
    <option value=3>Water</option>
</select>

Any advice would be awesome. 任何建议都很棒。 I've been playing around with this for hours and just can't get it to work 我已经玩了几个小时了,只是无法正常工作

Try changing 尝试改变

var building = $('#utilityMainChart').val();

to

var building = $('#utilityMainChart :selected').val();

EDIT: Your #utilityMainChart is not a unique id it is on both the select and the selects containing div. 编辑:您的#utilityMainChart不是唯一的ID,它同时在select和包含div的#utilityMainChart上。 So you when you run the above code it gets the div. 因此,当您运行上述代码时,它将获得div。 If you try this will it help. 如果您尝试这样做会有所帮助。

var building = $('select#utilityMainChart').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM