我正在尝试编写一个 php 脚本,该脚本将根据主下拉菜单的选择来填充第二个下拉菜单.我想用 jquery 来做所有非页面刷新的事情.但是我发现存在的每一件事都很难理解和修改,你知道任何写得很好且易于理解的东西,或者可能存在的教程吗?
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'
);
});
});
这篇关于php下拉菜单填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
PHP、MySQL PDOException 的死锁异常代码?Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL 可滚动游标不起作用PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC 连接PHP PDO ODBC connection(PHP PDO ODBC 连接)
使用 PDO::FETCH_CLASS 和魔术方法Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
php pdo 只从 mysql 获取一个值;等于变量的值php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
MSSQL PDO 找不到驱动程序MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)