Jquery,在ajax成功函数中操作数据
jquery, manipulate the data in an ajax success function
在qwerty.php中,我能够用ajax加载test.php,我还想操纵成功函数中加载的数据,但它似乎不起作用。
在qwerty.php我的代码是
<div class="qwerty">qwerty</div>
<script src="jquery.js"></script>
<script>
$(function(){
$.ajax({
type: 'GET',
url: 'test.php',
success: function(data, textStatus, jqXHR) {
$(data).find('.test1').click(function(){$(this).toggle();});
$('.qwerty').before(data);
}
});
});
</script>
在test.php中我有:
<div class="test1">test1</div>
<div class="test2">test2</div>
所以我的数据在div
"qwerty"之前正确加载,但是当我点击"test1"时,什么都没有发生。
它不工作,因为你附加onclick到$(data)
,但插入data
到DOM。试试这个:
success: function(data, textStatus, jqXHR) {
var el = $(data);
el.find('.test1').addBack().filter('.test1').click(function(){$(this).toggle();});
$('.qwerty').before(el);
}
我使用find(...).addBack().filter(...)
来查找匹配您的选择器的所有元素。find()
将只发现test1和test1的子元素具有类testone。filter()
只会过滤所有的根元素,所以如果你有子元素,它不会在这些元素中搜索。因此构造find(...).addBack().filter(...)
使用filter()
,因为它是返回的字符串
试试这个
success: function(data, textStatus, jqXHR) {
$('.qwerty').before(data);
$(data).filter('.test1').click(function(){$(this).toggle();});
}
首先附加数据并在委托事件上使用…
success: function(data, textStatus, jqXHR) {
$('.qwerty').before(data);
}
}); //ajax end
$('.qwerty').on('click' , '.test1' ,function(){
$(this).toggle();
});
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 如何确定javascript已经完成了某些操作.ios上的
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 从JavaScript访问struts操作中的属性
- fluxxor向一个flux实例添加一组以上的操作
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 操作放置在画布上的元素之间的连接
- 使用“+="操作人员
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- Jquery未定义函数正在停止其他操作
- 如何操作iframe之外的元素
- javascript对象操作:根据指定条件选择属性
- jQuery:暂停按钮可以暂停所有其他操作
- 用于选择/文本框操作的JavaScript
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- 通过javascript操作图像,非常简单
- 将文本框中的值用于按钮窗体操作上的变量
- 当通过AJAX调用时,控制器操作不会执行
- 多于两个操作数的OR操作