Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在两个不同的div ID中有两个同名的HTML下拉列表,那么如何使用AJAX/jQuery提交特定div ID的下拉列表值呢_Php_Jquery_Ajax - Fatal编程技术网

Php 在两个不同的div ID中有两个同名的HTML下拉列表,那么如何使用AJAX/jQuery提交特定div ID的下拉列表值呢

Php 在两个不同的div ID中有两个同名的HTML下拉列表,那么如何使用AJAX/jQuery提交特定div ID的下拉列表值呢,php,jquery,ajax,Php,Jquery,Ajax,下面是我根据所选国家填充州列表的代码。当我从“国家”下拉列表中选择任何国家时,它会在“国家”下拉列表中返回正确的国家列表。这部分工作正常 当我选择country USA时,“州”下拉列表将填充美国各州,默认选中的州是阿拉斯加。现在,如果我从states下拉列表中选择另一个州“Texas”,并使用AJAX/jQuery提交表单,它只会在表单POST数据中发送值0 我认为这个0值来自-Select State-of id=before\u get\u State。我想这样做,当提交FrMySpRIP

下面是我根据所选国家填充州列表的代码。当我从“国家”下拉列表中选择任何国家时,它会在“国家”下拉列表中返回正确的国家列表。这部分工作正常

当我选择country USA时,“州”下拉列表将填充美国各州,默认选中的州是阿拉斯加。现在,如果我从states下拉列表中选择另一个州“Texas”,并使用AJAX/jQuery提交表单,它只会在表单POST数据中发送值0

我认为这个0值来自-Select State-of id=before\u get\u State。我想这样做,当提交FrMySpRIPGIN地址时,它应该考虑从ID= SuthGeTySt. 那么我如何解决这个问题呢?请帮忙

国家/地区和州下拉列表

产品\地址\获取\状态\ p.php页面代码

此处提交product_address.js表格


因为您有两个id相同的选择框,所以它只获取第一个选择框的值。相反,上面的另一种方法是使用$after_get_state.findselect.val直接获取值。另外,您可以检查div,即:after\u get\u state是否在其内部有select或未使用.length。因此,如果length的值大于0,则将此选择框值发送到其他

演示代码:

console.loglength=+$after\u get\u state.findselect.length //检查div是否包含select 如果$after\u get\u state.findselect.length>0{ //那里..发送此值 console.logslected value=+$after_get_state.findselect.val }否则{ //没有发送其他选择框值 } -选择状态- -选择状态- DCDC
您好,更改$selectcbo_state.val;到$after_get_state.findselect[name=cbo_state].val或仅$after_get_state.findselect.val并检查一下。@Swati,成功了,请将您的答复作为答案发布,以便我可以选择它。。。很奇怪much@Swati,还有没有办法设置if条件,以便我可以发送prefore_get_state的cbo_state值或after_get_state的cbo_state值?请告诉我您是如何知道要发送哪个值的?@swati,当会员登录时,如果会员在其个人资料中设置了国家/州,则默认情况下,这些国家/州值将显示在此页面上。在这种情况下,成员不会更改国家,因此在onChange事件上不会调用get_state函数,因此我将不会得到after_get_state的cbo_state值,因为after_get_state div中的代码从未被调用。请让我知道我是否解释正确。
<form name="frm_shipping_address" id="frm_shipping_address" novalidate>
    <label>Select Country</label><span class="text-help-form"> * </span>
    <select name="cbo_country" id="cbo_country" class="form-control" onChange="get_state(this.value);">
    -- Country list is filled from MySQL database using while loop -- 
    </select>

    <label>Select State</label><span class="text-help-form"> * </span>
    <div id="before_get_state">
        <select name="cbo_state" id="cbo_state" class="form-control">
            <option value="0">-- Select State --</option>
        </select>
    </div>
    <div id="after_get_state"></div>    
    <button id="btn_continue" class="btn btn-primary btn-lg btn-block"><b>SUBMIT&nbsp;</b></button>
</form>
<script>
function get_state(countrypkid) {
    var int_countrypkid = countrypkid;
    var dataString = "countrypkid="+int_countrypkid;
    $.ajax({
        type: "POST",
        url: "./product_address_get_state_p.php",
        data: dataString,
        success: function(html)
        {
            $("#before_get_state").hide();
            $("#after_get_state").html(html);
        }
    });
}
</script>
-- Fetching states list from MySQL table for selected country pkid --
-- States list is filled from MySQL database using while loop -- 
(function() {
$("#frm_shipping_address input, #frm_shipping_address textarea, #frm_shipping_address select").jqBootstrapValidation({
    preventSubmit: true,
    submitSuccess: function($form, event) {
        event.preventDefault();
        var cbo_country = $("select#cbo_country").val();
        var cbo_state = $("select#cbo_state").val();
        
        $.ajax({
            url: "./product_address_p.php",
            type: "POST",
            data: {
                cbo_country: cbo_country,
                cbo_state: cbo_state
            },
            cache: false,
            success: function(data) 
            { 
                var $responseText=JSON.parse(data);
                if($responseText.status == 'SUC')
                {
                    // Success message
                }
                else if($responseText.status == 'ERR')
                {
                    // Fail message
                }
            },
        })
    },
});
});