[英]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 您也可以从中弄清楚如何去做城市
Please check this tutorial. 请检查本教程。 it will definitely help you.
它肯定会帮助您。
http://www.php-dev-zone.com/2013/10/country-state-city-dropdown-using-ajax.html http://www.php-dev-zone.com/2013/10/country-state-city-dropdown-using-ajax.html
Or this one. 还是这个。
http://www.techsofttutorials.com/ajax-country-state-city-dropdown-using-phpmysql/ http://www.techsofttutorials.com/ajax-country-state-state-city-dropdown-using-phpmysql/
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.