"One" ); echo json_encode( $arr -6ren">
gpt4 book ai didi

php - 如何在下拉更改事件中调用 AJAX 请求?

转载 作者:搜寻专家 更新时间:2023-10-31 20:55:14 24 4
gpt4 key购买 nike

index.php

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>

<a href='one.php' class='ajax'>One</a>
<a href='two.php' class='ajax'>Two</a>

<div id="workspace">workspace</div>

one.php

$arr = array ( "workspace" => "One" );
echo json_encode( $arr );

two.php

$arr = array( 'workspace' => "Two" );
echo json_encode( $arr );

ajax.js

jQuery(document).ready(function(){
jQuery('.ajax').live('click', function(event) {
event.preventDefault();
// load the href attribute of the link that was clicked
jQuery.getJSON(this.href, function(snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
jQuery('#' + id).html(snippets[id]);
}
});
});
});

以上代码运行良好。当我单击链接“一”时,字符串“一”将加载到工作区 DIV 中,当我单击链接“二”时,字符串“二”将加载到工作区 DIV 中。

问题:

现在我想使用下拉菜单在工作区 DIV 中加载 one.php 和 two.php,而不是在 index.php 中加载链接。当我使用链接时,我在链接属性中使用 class='ajax' 但是如何在下拉更改事件中调用 ajax 请求?

谢谢

最佳答案

像这样更改您的代码:

jQuery('#dropdown_id').live('change', function(event) {
jQuery.getJSON($(this).val(), function(snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
jQuery('#' + id).html(snippets[id]);
}
});
});

你的下拉菜单应该是这样的:

<select id="dropdown_id">
<option value="one.php">One</option>
<option value="two.php">Two</option>
</select>

关于php - 如何在下拉更改事件中调用 AJAX 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com