【问题标题】:Why doesn't a newly populated drop-down menu trigger the AJAX change function?为什么新填充的下拉菜单不会触发 AJAX 更改功能?
【发布时间】:2014-05-09 14:30:23
【问题描述】:

我目前正在实现一个包含 AJAX 请求的三重依赖下拉菜单(想想国家->州->城市)。

这里是我的下拉结构的代码sn-p:

        //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="0">---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">';
    echo '<option selected="selected">---Select a webproperty---</option>';
    echo '</select>';

    //for available profiles
    echo '<br> Available Profiles: ';
    echo '<select name="dropProfiles" class="dropProfiles">';
    echo '<option selected="selected">---Select a profile---</option>';
    echo '</select>';

重要的变量是dropAccounts(国家)、dropProperties(州)和dropProfiles(城市)。第一个下拉列表由 API 调用填充,然后 AJAX 请求在 onchange 事件中从其中获取值:

<script type="text/javascript">
    $(document).ready(function()
    {
        $(".dropAccounts").change(function()
        {
            var accountID = $(this).val(); //gets the account ID from drop-down value

            $.ajax
            ({
                type: "POST",
                url: "propertyID.php",
                data: {
                    'accountID' : accountID
                },
                cache: false,
                success: function(html)
                {
                    $(".dropProperties").html(html);
                } 
            });

        });

    });
</script>

然后 propertyID.php 然后像这样填充dropProperties(假设我正在从数据库中获取值):

if($_POST['accountID'])
{  
    if ($accountID != "0") {
        foreach ($webItem as $item) {
            echo '<option value=' . $item->getId() . '>' . $item->getName() . '</option>';
        }
    }
}
else {
    echo '<option value="0">---Select a webproperty---</option>';
}

我同样设置了第三个下拉菜单 (dropProfiles) 以完全相同的方式填充,假设当第二个下拉菜单重新填充时它会触发 javascript onchange 事件。但是,当我让第二个下拉列表重新填充时,它不会执行第三个下拉列表的 javascript。

这是应该触发 PHP 脚本填充 dropProfiles 的 javascript onchange 函数:

<script type="text/javascript">
    $(document).ready(function()
    {
        $(".dropProperties").change(function()
        {
            var id = $(this).val(); //gets the profile ID from drop-down value

            $.ajax
            ({
                type: "POST",
                url: "profileID.php",
                data: {
                    'id' : id
                },
                cache: false,
                success: function(html)
                {
                    $(".dropProfiles").html(html);
                } 
            });

        });

    });
</script>

有解决办法吗?我是不是走错了路?

【问题讨论】:

    标签: javascript php jquery ajax onchange


    【解决方案1】:

    当您想要加载级联值时,最好的办法是手动调用“填充”函数。为此,您需要将它们分解为命名函数。这使您能够在任何时候触发下拉填充。

    $(document).ready(function()
    {
    
        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());
                } 
            });
    
        }
    
        function populateProfiles(propertyId) {
            $.ajax
            ({
                type: "POST",
                url: "profileID.php",
                data: {
                    'id' : propertyId
                },
                cache: false,
                success: function(html)
                {
                    $(".dropProfiles").html(html);
                } 
            });
        }
    
        $(".dropAccounts").change(function()
        {
            var accountID = $(this).val(); //gets the account ID from drop-down value
            populateProperties(accountID);
        });
    
        $(".dropProperties").change(function()
        {
            var id = $(this).val(); //gets the profile ID from drop-down value
            populateProfiles(id);
        });
    
        // Call populateProperties on page load to kick things off
        populateProperties($(".dropAccounts").val());
    });
    

    【讨论】:

    • 谢谢@Will,我会试一试!
    • @Will 还是不行,填充第二个下拉列表的函数工作正常,但是第三个 PHP 脚本仍然没有执行。
    • 我可能有错字什么的。我会测试一下,看看我能不能找出问题所在。
    • @WillAnderson 感谢所有帮助!如果它可以帮助您进行调试,我认为$(".dropProperties").change() 根本不会执行,因为我认为代码正在替换整个下拉列表。不过可能是错的。
    • 我想我找到了。 'accountID' : accountID 应该是 'accountID' : accountId。随着这种变化,下拉菜单似乎可以正确级联。我用修复更新了我的答案
    【解决方案2】:

    代码中的值更改不会触发事件

    onchange 事件必须由用户操作触发或由脚本显式触发。

    您可能会受益于使用jQuery's .trigger() method

    可以在这里找到类似的问题和答案:Trigger "onchange" event

    【讨论】:

    • 感谢您的建议!我对 JS 和 AJAX 很陌生,是否仍然可以使用 .trigger() 方法发出 AJAX 请求?
    • 您不会使用 .trigger() 发出 AJAX 请求,但如果您的 AJAX 请求位于由 onchange 请求触发的函数内,您可以使用 .trigger 手动触发 onchange 事件() 方法。
    • 另外,如果您的第三个下拉列表依赖于第二个下拉列表,那么仅用户进行选择的行为就应该触发使 AJAX 请求获取您的第三个的函数菜单的内容。因此,在这种情况下,甚至不需要以编程方式触发 onchange 事件。
    • 对于您的用例,我认为@WillAnderson 的答案是正确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多