本文介紹了php下拉菜單填充的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試編寫一個(gè) php 腳本,該腳本將根據(jù)主下拉菜單的選擇來填充第二個(gè)下拉菜單.我想用 jquery 來做所有非頁面刷新的事情.但是我發(fā)現(xiàn)存在的每一件事都很難理解和修改,你知道任何寫得很好且易于理解的東西,或者可能存在的教程嗎?
I'm trying to write a php script that will populate a second drop down menu based on the selection of the a primary drop down menu. I would like to use jquery to do all the non-page refresh stuff. but every thing that I find that exists out there is hard to understand and modify, do you know of anything that was well written and easy to understand or maybe a tutorial that exists out there?
推薦答案
以下是一些代碼,可以讓您了解您想要做什么:
Here's some code that should give you an idea of what you want to do:
HTML
<select id="state" name="state">
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
</select>
<select id="city" name="city">
<option value="">Please select a state...</option>
</select>
PHP
<?php
$cities = array(
'IL' => array( 'Chicago', 'Naperville', 'Decatur', 'Saint Charles' ),
'IN' => array( 'Gary', 'Miller', 'Portage', 'Merrillville' )
);
print json_encode( $cities[ $_POST[ 'state' ] ] );
exit;
?>
jQuery
jQuery(document).ready(function() {
jQuery('#state').change(function() {
jQuery.post(
'some-url.php',
{
'state':jQuery('#state').val()
},
function(data, textStatus) {
jQuery.each(data, function(index, value) {
jQuery('#city').append('<option value="' + value + '">' + value + '</option>');
});
},
'json'
);
});
});
這篇關(guān)于php下拉菜單填充的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!