Jquery Submit按钮通过ajax发布数据,由createhtml制作,不起作用
我要怎么做才能让第二个按钮工作?第一个按钮运行平稳,但第二个按钮运行不平稳,如果单击test_链接,它将显示第二个按钮,该按钮由javascript创建Jquery Submit按钮通过ajax发布数据,由createhtml制作,不起作用,jquery,ajax,button,Jquery,Ajax,Button,我要怎么做才能让第二个按钮工作?第一个按钮运行平稳,但第二个按钮运行不平稳,如果单击test_链接,它将显示第二个按钮,该按钮由javascript创建 <script src="http://localhost/googledrive/www/ci/2.1.3/social/assets/jquery/jquery-1.7.1.js"></script> <script type="text/javascript" > $(documen
<script src="http://localhost/googledrive/www/ci/2.1.3/social/assets/jquery/jquery-1.7.1.js"></script>
<script type="text/javascript" >
$(document).ready(function()
{
$(".comment_button").click(function(){
$.ajax({
type: "POST",
dataType : "json",
url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test",
data: { name: "John", time: "2pm" },
success : function(cont) {
if(cont){
$.each(cont, function(key, val) {
alert(key+" "+val);
});
}
}
});
return false;
});
$(".comment_link").click(function(e){
e.preventDefault();
var element = $(this);
var id = element.attr("post_id");
$("#"+id).html('<input><button class="comment_button">Second button, not work</button>');
return false;
});
});
</script>
<form method="post" action="http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test/2">
<button class="comment_button">first button, this_button works</button>
</form>
<div id="7"></div>
<a href="http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test/2" class="comment_link" post_id="7">test_link</a>
<br>
php code : <?php echo json_encode($_POST);?>
$(文档).ready(函数()
{
$(“.comment_按钮”)。单击(函数(){
$.ajax({
类型:“POST”,
数据类型:“json”,
url:“http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test",
数据:{姓名:“约翰”,时间:“下午2点”},
成功:功能(续){
如果(续){
$。每个(续,功能(键,值){
警报(按键+“”+val);
});
}
}
});
返回false;
});
$(“.comment_link”)。单击(函数(e){
e、 预防默认值();
var元素=$(此);
var id=element.attr(“post_id”);
$(“#”+id).html('Second button,not work');
返回false;
});
});
第一个按钮,这个按钮工作
php代码:
您正在设置。单击()
方法在将按钮添加到DOM之前,您可以重新定义JavaScript,如:
$(document).ready(function () {
$(".comment_link").click(function (e) {
e.preventDefault();
var element = $(this);
var id = element.attr("post_id");
$("#" + id).html('<input><button class="comment_button">Second button, not work</button>')
.children(".comment_button").click(function () {
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test",
data: {
name: "John",
time: "2pm"
},
success: function (cont) {
if (cont) {
$.each(cont, function (key, val) {
alert(key + " " + val);
});
}
}
});
return false;
});
return false;
});
});
将ajax调用移动到一个单独的非匿名函数,并从中调用它。单击()
事件链接,如前一个示例中所示…查看。on()
应该就是您要查找的。由于您的单击在HTML实际生成之前就准备好了,所以您使用的旧单击甚至不知道元素存在。on()会刷新元素以查找它们
$(document).ready(function () {
$(".comment_link").click(function (e) {
e.preventDefault();
var element = $(this);
var id = element.attr("post_id");
$("#" + id).html('<input><button class="comment_button">Second button, not work</button>')
.children(".comment_button").click(commentButtonClicked);
return false;
});
function commentButtonClicked() {
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost/googledrive/www/ci/2.1.3/social/index.php/talk/test",
data: {
name: "John",
time: "2pm"
},
success: function (cont) {
if (cont) {
$.each(cont, function (key, val) {
alert(key + " " + val);
});
}
}
});
return false;
}
});