第二个下拉菜单未选中 [英] second dropdown menu not selected

查看:102
本文介绍了第二个下拉菜单未选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户注册页面的下拉菜单,国家/地区状态。这里State是基于国家的。

I have two dropdown menu, country and state for the user registration page. Here State is based on country.

如果我选择国家/地区,那么基于该国家/地区的州将会过滤。

If I select the country, then the state based on that country will filter.

我的问题是在编辑用户时。

My problem is while editing a user.

我有以下jquery代码:

I have following jquery code:

var u_id = <?= intval($_GET['id']);?>;
        $.post('model/getUser.php',{id:u_id},function(data){
            if(data.success == true){
                 $("#country").val(data.result.country);
                dochange('state', data.result.country);
             $("#state").val("+data.result.state+").attr('selected',true);
            }
        },'json');

这里我要做的是一旦选择了国家,那么它将通过使用函数过滤其状态

Here what I am trying to do is once country is selected then it will filter its state by using function

function dochange(src, val) {
    var req = Inint_AJAX();
    req.open("GET", "loc.php?data="+src+"&val="+val);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    req.send(null);
}

loc.php

$data = $_GET['data'];
$val = $_GET['val'];
$val2 = $_GET['val2'];

if ($data=='country') {
    echo "<select  class='select-style' name='country' id=\"country\" onChange=\"dochange('state', this.value)\">";
    echo "<option value=''>- Select Country -</option>\n";
    $result=mysql_query("select * from countries order by countryName asc");
    while($row = mysql_fetch_array($result)){
        echo "<option value='$row[countryCode]'> $row[countryName]</option>" ;
    }
} 
else if ($data=='state') {
    echo "<select class='select-style' name='state'  id=\"stateId\" >\n";
    echo "<option value=''>- Select State -</option>\n";

    $result=mysql_query("SELECT * FROM state WHERE countryCode= '$val' order by stateName asc");
    while($row = mysql_fetch_array($result)){
        $selected = "";
        if ($val2 == $row[state]) $selected = " selected ";
        echo "<option ".$selected. " value=\"$row[state]\">$row[stateName]</option> " ;
    }
} 
echo "</select>\n";

它已被过滤,但我无法显示用户的状态。

It is getting filtered but I am not being able to display the state of the user.

任何人都可以帮我解决这个问题。

Can anybody please help me fix this problem.

谢谢。

推荐答案

您需要对函数dochange进行一些更改

You need to do some change on the function dochange

function dochange(src, val,cb) {
    var req = Inint_AJAX();

    req.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            if(cb)
                cb(this.responseText);
       }
    };
    req.open("GET", "loc.php?data="+src+"&val="+val);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    req.send(null);
}

然后你可以使用这个

var u_id = <?= intval($_GET['id']);?>;
    $.post('model/getUser.php',{id:u_id},function(data){
        if(data.success == true){
             $("#country").val(data.result.country);
            dochange('state', data.result.country,function(data){
                $("#state").html(data);
            });

        }
    },'json');

你也可以这样使用

var u_id = <?= intval($_GET['id']);?>;
    $.post('model/getUser.php',{id:u_id},function(data){
        if(data.success == true){
             $("#country").val(data.result.country);
            dochange('state', data.result.country,function(data){
                $("#state").html(data);
                $("#state").val('01');
            });

        }
    },'json');

这篇关于第二个下拉菜单未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆