级联选择框 - Jquery

发布于 2024-10-18 05:01:47 字数 977 浏览 3 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2024-10-25 05:01:47

尝试更改

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

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

编辑:您的#utilityMainChart不是唯一的ID,它在选择和包含div的选择上都存在。所以当你运行上面的代码时,它会获取 div。如果你尝试这样做会有帮助。

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

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. So you when you run the above code it gets the div. If you try this will it help.

var building = $('select#utilityMainChart').val();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文