jQuery select menu ajax 3 levels

jQuery select menu ajax 3 levels

本文关键字:levels ajax menu select jQuery      更新时间:2023-09-26

我有 3 个应该相互连接的选择字段。

代码正常工作,但第二个选择除外,它显示正确的选择值,但在控制台日志中给出第一个选项值,即使在页面上显示第二个值仍处于选中状态。

这是我的jQuery代码

$(document).ready(function() {    
    $('#country').change(function(){
        var countryid = $(this).val();
        $.post('selectcity.php', {'countryid' : countryid}, function(data){
            if(data==0) {
                $('#city').attr('disabled','disabled'); 
            } else {
                $('#city').parent().find('.select_container').html(data);
                var city_id = $('#city').find(":selected").val();
                console.log(city_id);
            }
        });
    }).change();
});

.HTML

<select id="country" class="select_container">
 <option value="1">1</option>
</select>
<select id="city" class="select_container"></select>
<select id="item_id"  class="select_container " ></select>

谁能告诉我出了什么问题?

jQuery 代码是正确的,很抱歉打扰他们遇到的每个人,因为在 php 文件中,每个项目的值都相同,必须进行调整,它按照我想要的方式工作

这是旧的(错误的(PHP代码

    <?php 
if (isset($_REQUEST['countryid']) && $_REQUEST['countryid']!="") {
    $citynewval = $_REQUEST['countryid'] + 3;
    $citynewval2 = $_REQUEST['countryid'] + 5;
    $citynewval3 = $_REQUEST['countryid'] + 8;
    echo'<option value="'.$citynewval.'"';
    if (!(strcmp($citynewval, 6))) {echo ' selected';}
    echo '>'.$citynewval.'</option>';
    echo'<option value="'.$citynewval.'"';
    if (!(strcmp($citynewval2, 6))) {echo ' selected';}
    echo '>'.$citynewval2.'</option>';
    echo'<option value="'.$citynewval.'"';
    if (!(strcmp($citynewval3, 6))) {echo ' selected';}
    echo '>'.$citynewval3.'</option>';
    }else {
        echo 0;
        }?>

这是更正后的代码

    <?php 
if (isset($_REQUEST['countryid']) && $_REQUEST['countryid']!="") {
    $citynewval = $_REQUEST['countryid'] + 3;
    $citynewval2 = $_REQUEST['countryid'] + 5;
    $citynewval3 = $_REQUEST['countryid'] + 8;
    echo'<option value="'.$citynewval.'"';
    if (!(strcmp($citynewval, 6))) {echo ' selected';}
    echo '>'.$citynewval.'</option>';
    echo'<option value="'.$citynewval2.'"';
    if (!(strcmp($citynewval2, 6))) {echo ' selected';}
    echo '>'.$citynewval2.'</option>';
    echo'<option value="'.$citynewval3.'"';
    if (!(strcmp($citynewval3, 6))) {echo ' selected';}
    echo '>'.$citynewval3.'</option>';
    }else {
        echo 0;
        }?>

感谢那些试图提供帮助的人