级联选择框 - Jquery
使用 jQuery 动态填充选择框时遇到问题。
基本上,我的代码调用一个 php 脚本来填充选择框 (utilityMainChart)。假设它为选择框 utilityMainChart 返回两个选项,我希望下一个选择框(parameterMainChart)填充 utilityMainChart 选择框中第一个选项的选项。
现在,选择框最初会正确填充,但行 varbuilding = $('#utilityMainChart').val();永远不会返回选择框的值。这意味着对选择框的任何更改都会导致其下方的选择框变为空白,因为没有选取任何值。
我的 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);
我的 php 代码返回以下形式的数据:
<select id="utilityMainChart" name="utilityMainChart">
<option selected value=1>Electricity</option>
<option value=3>Water</option>
</select>
任何建议都会很棒。我已经玩了好几个小时了,但就是无法让它工作
Having a problem with dynamically populated select boxes using jQuery.
Basically, my code calls a php script which populates the select box (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.
As it is right now, the select boxes get populate properly initially, but the line 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:
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:
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改
为
编辑:您的
#utilityMainChart
不是唯一的ID,它在选择和包含div的选择上都存在。所以当你运行上面的代码时,它会获取 div。如果你尝试这样做会有帮助。Try changing
to
EDIT: Your
#utilityMainChart
is not a unique id it is on both the select and the selects containing div. So you when you run the above code it gets the div. If you try this will it help.