【发布时间】:2014-05-09 20:54:26
【问题描述】:
我正在构建一个 Web 应用程序,该应用程序将利用三重相关下拉菜单(想想国家 -> 州 -> 城市)并允许用户更改他们的详细信息。
这里是下拉结构的代码sn-p(注意dropAccounts的默认选项的值是'test'):
//create a drop down of available accounts
echo 'Available Accounts: ';
echo '<select name="dropAccounts" class="dropAccounts">';
//if there is at least one account available
if (count($accsAvailable) > 0) {
echo '<option value="test">---Select an account---</option>'; //default option
foreach ($accsAvailable as $account) {
//populate from API
echo '<option value=' . $account->getId(). '>' . $account->getName() . '</option>';
}
} else {
echo '<option value="0">---No accounts available---</option>'; //else if no accounts exist
}
echo '</select>';
//for available webproperties
echo '<br> Available Webproperties: ';
echo '<select name="dropProperties" class="dropProperties" id="dropProperties">';
echo '<option selected="selected">---Select a webproperty---</option>';
echo '</select>';
//for available profiles
echo '<br> Available Profiles: ';
echo '<select name="dropProfiles" class="dropProfiles" id="dropProfiles">';
echo '<option selected="selected">---Select a profile---</option>';
echo '</select>';
我正在使用 onchange 事件和 AJAX 从第一个下拉菜单 dropAccounts(国家/地区)中提取值,并使用该值来索引 API 调用以填充第二个下拉菜单, dropProperties(状态)如下:
$(".dropAccounts").change(function()
{
var accountID = $(".dropAccounts").val(); //gets the account ID from drop-down value
populateProperties(accountID);
});
function populateProperties(accountID) {
$.ajax
({
type: "POST",
url: "propertyID.php",
data: {
'accountID' : accountID
},
cache: false,
success: function(html)
{
$(".dropProperties").html(html);
// Populate profiles after properties load
populateProfiles($(".dropProperties").val());
}
});
}
AJAX 请求替换 dropProperties(状态)下拉列表的内容,然后调用 populateProfiles() 函数以类似方式填充最终下拉列表 dropProfiles(城市):
function populateProfiles(propertyID) {
$.ajax
({
type: "POST",
url: "profileID.php",
data: {
'propertyID' : propertyID
},
cache: false,
success: function(html)
{
$(".dropProfiles").html(html);
}
});
}
此方法可以正常使用正确的dropAccounts(国家/地区)和dropProperties 填充前两个下拉列表,但是在第三个下拉列表dropProfiles(城市)中检索到的值不是来自@ 的值987654336@.
在 profileID.php 脚本中:
<?php
$propertyID = $_POST['propertyID'];
echo '<option> Property ID: ' . $propertyID . '</option>';
?>
$propertyID 的值从 dropAccounts 的默认选项返回为 'test'。我已经仔细检查了所有变量,我很困惑。我想知道是否有经验更丰富的人可以找出问题所在?
提前致谢!
【问题讨论】:
标签: javascript php jquery html ajax