[英]how to change a select list option when other select list changed?
I have two selectlists in my html and both have the values saved in database, I want to know how to change values in selectlist 2 when select 1 is changed by user using jquery? 我的html中有两个选择列表,两个都保存在数据库中,我想知道当用户使用jquery更改select 1时,如何更改selectlist 2中的值?
I am trying to do these things in codeigniter, so please help me, I have done these things using jquery before but that was in simple php by passing the values to the load function of jquery, but this is not working in codeigniter, anyone please help me 我试图在codeigniter中做这些事情,所以请帮助我,我之前使用jquery完成了这些事情,但是这是在简单的PHP中通过将值传递给jquery的加载函数,但这在codeigniter中不起作用,任何人都请帮我
Thanks, 谢谢,
Shah RUkh Shah RUkh
Say for example you have this drop down 比如说你有这个下拉菜单
<select id="select_1">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select id="select_2">
<option value="">Select</option>
</select>
On change of select_1 drop down you should get the value and do a AJAX request to your controller function which will return you the options for select_2 than simply replace the html of select_2 with the returned response from script 在更改select_1下拉列表时,您应该获取值并向控制器函数执行AJAX请求,这将返回select_2的选项,而不是简单地将select_2的html替换为来自脚本的返回响应
$('#select_1').change(function(){
var id = $(this).val();
$.ajax({
type: "POST",
url: base_url+'controller/action/',
data: 'id='+id,
success: function(html){
$('#select_2').html(html);
}
});
});
For codeignitor use the base_url in the url parameter of ajax request. 对于codeignitor,请在ajax请求的url参数中使用base_url。 You can define a CDATA
variable for base_url which you can use in your ajax requests, you can define it in your header.php file. 您可以为base_url定义一个CDATA
变量,您可以在ajax请求中使用该变量,您可以在header.php文件中定义它。
<script type="text/javascript">
//<![CDATA[
base_url = '<?php echo base_url();?>';
//]]>
</script>
Okay I have done it, using jQuery 好吧,我已经使用jQuery完成了它
<script type="text/javascript">
$(document).ready(function()
{
$("#slect_1").change(function()
{
id = $("#select_1").val();
$("#select_2").load("<?php echo site_url('class/method'); ?>/"+id);
});
});
</script>
Thanks, 谢谢,
Shah Rukh 沙鲁克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.