简体   繁体   English

使用JQuery和Ajax发布在表单中选择另一个下拉菜单时更改下拉列表值

[英]Change dropdown value when another dropdown selected in a form using JQuery & ajax post

how to change a dropdown value based on another dropdown value? 如何根据另一个下拉值更改下拉值?

I will have 3 dropdown value in a form just like below: 我将使用以下形式的3个下拉值:

<form method="post" action="find.pgp"><div class="form-group col-lg-2">
            <label>Country</label>
            <select id="country" name="country" class="form-control">
                <option value="1">Japan</option>
                <option value="2">China</option>
                <option value="3">New Zealand</option>
            </select>
        </div>
        <div class="form-group col-lg-2">
            <label>province</label>
            <select name="province" class="form-control">
                <option value="1">a province</option>
            </select>
        </div>

<div class="form-group col-lg-2">
            <label>city</label>
            <select name="city" class="form-control">
                <option value="1">a city</option>
            </select>
        </div> <input type="submit> </form>

What I want is, 我想要的是
1st I choose a country name 首先我选择一个国家名
2nd Province Changed into based on country relation to it's table on db 根据数据库上的国家/地区关系,将第二省更改为
3rd I choose province then value of city dropdown changed into city which has relation to province table in database 第三,我选择省,然后将城市下拉列表的值更改为与数据库中的省表有关的城市
4th I will submit all this to find something in db 4th我将提交所有这些以在db中找到某些东西

So what I am supposed to do with JQuery and Ajax to retrieve value and change the dropdown value? 那么我应该如何使用JQuery和Ajax来获取值并更改下拉值呢? Thank you 谢谢

So basically you need to disable select first unless for country right? 因此,基本上您需要先禁用“ select除非国家/地区适合您? Or something else that would make the country field selected first. 否则将使“国家/地区”字段首先被选中。

<form id="myForm">
    <div class="form-group col-lg-2">
        <label>Country</label>
        <select id="country" name="country" class="form-control">
            <option value="1">Japan</option>
            <option value="2">China</option>
            <option value="3">New Zealand</option>
        </select>
    </div>
    <div class="form-group col-lg-2">
        <label>province</label>
        <select name="province" class="form-control" disabled>
            <option value="1">a province</option>
        </select>
    </div>

    <div class="form-group col-lg-2">
        <label>city</label>
        <select name="city" class="form-control" disabled>
            <option value="1">a city</option>
        </select>
    </div>
    <input type="submit">
</form>

As I don't know what is your server response is. 我不知道您的服务器响应是什么。 I'm assuming as this one 我假设是这个

{"response": " <option selected value=\"countryprovince1\">Selected Province1</option><option selected value=\"countryprovince2\">Selected Province2</option><option selected value=\"countryprovince3\">Selected Province3</option>"}

And by this means, I can simply use jQuery html() 通过这种方式,我可以简单地使用jQuery html()

    //Select country first
$('select[name="country"]').on('change', function() {
    var countryId = $(this).val();

    $.ajax({
        type: "POST",
        url: "get-province.php",
        data: {country : countryId },
        success: function (data) {
                    //remove disabled from province and change the options
                    $('select[name="province"]').prop("disabled", false);
                    $('select[name="province"]').html(data.response);
        }
    });
});


$('select[name="province"]').on('change', function() {
    var provinceId = $(this).val();

    $.ajax({
        type: "POST",
        url: "get-city.php",
        data: {province : provinceId },
        success: function (data) {
                    //remove disabled from city and change the options
                    $('select[name="city"]').prop("disabled", false);
                    $('select[name="city"]').html(data.response);
        }
    });
});

//once all field are set, submit
$('#myForm').submit(function () { 
    $.ajax({
        type: "POST",
        url: "find.php",
        data: $('#myForm').serialize(),
        success: function (data) {
                //success
        }
      });
    });
});

First add an id to your province select and your city select 首先向您select的省份和城市select一个id

<form method="post" action="find.pgp">
    <div class="form-group col-lg-2">
        <label>Country</label>
        <select id="country" name="country" class="form-control">
            <option value="1">Japan</option>
            <option value="2">China</option>
            <option value="3">New Zealand</option>
        </select>
    </div>

    <div class="form-group col-lg-2">
        <label>province</label>
        <select name="province" class="form-control" id="province">
        </select>
    </div>

    <div class="form-group col-lg-2">
        <label>city</label>
        <select name="city" class="form-control" id="select"></select>
    </div>
    <input type="submit">
</form>

Then, assuming you already have jQuery setup on the page: 然后,假设您已经在页面上安装了jQuery:

<script>
    $(function(){
        // event called when the country select is changed
        $("#country").change(function(){
            // get the currently selected country ID
            var countryId = $(this).val();
            $.ajax({
                // make the ajax call to our server and pass the country ID as a GET variable
                url: "get_provinces.php?country_id=" + countryId,
            }).done(function(data) {
                // our ajax call is finished, we have the data returned from the server in a var called data
                data = JSON.parse(data);

                // loop through our returned data and add an option to the select for each province returned
                $.each(data, function(i, item) {
                    $('#province').append($('<option>', {value:i, text:item}));
                });

            });
        });
    });
</script>

And your get_provinces.php script which you are calling with ajax: 以及使用ajax调用的get_provinces.php脚本:

<?php
    /// we can access the country id with $_GET['country_id'];
    // here you can query your database to get the provinces for this country id and put them in an array called $provinces where the key is the id and the value is the province name

    // this is a dummy array of provinces, you will replace this with the data from your database query
    $provinces = array(6=>"Province One",54=>"Province Two",128=>"Province Three");
    echo json_encode($provinces);
?>

That's the basic idea. 这是基本思想。 You will obviously need to change your get_provinces.php to query your database and return the correct data using the country id. 显然,您将需要更改get_provinces.php来查询数据库并使用国家/地区ID返回正确的数据。 You'll be able to figure out how to do the cities from this as well 您也可以从中弄清楚如何去做城市

You have to use .change() event handler for this purpose 为此,您必须使用.change()事件处理程序

    $(document).ready(function() {
    $('.form-group col-lg-2').change(function() {
    var $select = $(this).val();
    // here you can apply condition on $select to apply different scenarios.


     });
   });

This is just an idea. 这只是一个想法。 You can have a look on different examples available online. 您可以查看在线上提供的其他示例。 Please have a look on the following webpage to get an idea of this functionality with database. 请浏览以下网页,以了解此功能与数据库。

https://css-tricks.com/dynamic-dropdowns/ https://css-tricks.com/dynamic-dropdowns/

Use just these two lines, it's working perfect. 仅使用这两行,就可以完美工作。

jQuery('#select_selector').change(function(){
  jQuery("#select_selector1 option").eq(jQuery(this).find(':selected').index()).prop('selected',true);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择下拉列表时,jQuery / AJAX填充HTML表单 - Jquery/AJAX populated HTML form when dropdown selected 无法使用$ .ajax将下拉列表选择的值发布到服务器 - Cannot post dropdown list selected value to server using $.ajax 如何使用jquery / ajax在文本框中显示下拉列表的选定值 - how to show selected value of dropdown into textbox using jquery/ajax 仅当选择特殊值时,才根据下拉列表的更改提交表单 - Submit form on change of dropdown only when special value is selected 获取 jquery ajax 中下拉列表的选定值 - Get selected value of dropdown in jquery ajax 使用ajax将所选下拉列表的值作为参数传递给另一个下拉列表中的函数 - Use ajax to pass the value of selected dropdown as an argument to a function in another dropdown 如果从下拉列表中选择了值,则使用 jquery 显示另一个 div - if value is selected from dropdown show another div using jquery 根据 React 中另一个下拉列表中选择的值更改下拉列表中的值 - Change value on a dropdown on the basis of a value selected in another dropdown in React 当从下拉菜单中选择选项时,如何使用jQuery将所选选项隐藏到另一个下拉菜单中 - When selected option from dropdown then how to hide that selected option into another dropdown using jQuery 如何使用aspnet webforms中的ajax根据另一个下拉列表选择值绑定下拉列表 - How to bind a dropdown list according to another dropdown selected value using ajax in aspnet webforms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM