jQuery触发器更改与链式ajax选择 [英] jquery trigger change with chained ajax select
本文介绍了jQuery触发器更改与链式ajax选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以这种方式使用了选择过的选择,效果很好.
I'm using a chanied select in this way and works very well.
$(function() {
/**
* Chained Select (id_foo)
*
* @method on change
*/
$('select[name="id_foo"]').on('change', function() {
var id_foo = $("option:selected", this).prop("value");
$.ajax({
type : "POST",
url : ajax.php,
data : { id_foo: id_foo },
success : function(data) {
$('select[name="id_bar"]').html(data);
}
});
});
}); /* END */
HTML
<select name="id_foo">
<option value="1">one</option>
<option value="2">two</option>
</select>
<br>
<select name="id_bar">
</select>
AJAX.PHP
if(isset($_POST['id_foo'])){
$obj->selectBar($_POST['id_foo']);
}
现在我想以这种方式使用触发功能来模拟变更事件
Now I'd like to use trigger function to simulate on change event in this way
$(function() {
$('select[name="id_foo"]').val('2').trigger('change');
/**
* Chained Select (id_foo)
*
* @method on change
*/
$('select[name="id_foo"]').on('change', function() {
...
...
但是没有成功.select的值为2,但触发事件不执行任何操作.我该如何解决?谢谢
But with no success. The value of select is 2 but the trigger event does nothing. How could I solve? thank you
推荐答案
val()不返回一个jQuery对象.
val() does not return a jQuery object.
相反
$(function() {
var $sel = $('select[name="id_foo"]');
$sel.on('change', function() {
var id_foo = this.value;
$.ajax({
type : "POST",
url : ajax.php,
data : { id_foo: id_foo },
success : function(data) {
$('select[name="id_bar"]').html(data);
}
});
});
$sel.val('2');
$sel.change();
});
这篇关于jQuery触发器更改与链式ajax选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文