HTML
echo '<form id="f_'.$id.'" method="post">';
echo '<input type="hidden" name="re_ti" value="'.$ti.'">';
echo '</form>';
echo '<button id="rBtn" l="#f_'.$id.'" class="button_accept" onclick="document.getElementById(\'review\').style.display = \'block\';">Review</button>';阿贾克斯
$(document).ready(function(){
$('#rBtn').click(function() {
var log = $(this).attr("l");
alert(l);
$.ajax({
type:"post",
url:"php_parsers/review.php",
data: $(l).serialize(),
success: function(response){
$("#reviewMain").html(response);
}
});
$.ajax({
type:"post",
url:"php_parsers/comment.php",
data: $(l).serialize(),
success: function(response){
$("#reviewSub").html(response);
}
});
});
});所以HTML代码与PHP相呼应,每个表单都有一个不同的id。
示例表单id是#f_234324 (它是计数)。
表单是使用while回显的,如果只有一个回显,一切都会正常工作,但是如果回显的表单多于一个,那么我的Ajax就不能工作。
Ajax应该获得l并使用l来更改数据:到我想要的论坛。
如果有人知道更好的解释方法,请告诉我。如果只有一种形式,则会弹出警报,但如果有两种形式,则不会弹出警报。
我遗漏了什么?
发布于 2015-09-24 06:47:48
我认为问题在于,您的文档中有多个rBtn id,因为如果php中的while循环生成多个按钮,那么您就有多个id rBtn。您应该使用唯一的id。我会在选择器中使用类名:
$(document).ready(function(){
$('.button_accept').click(function() {
var log = $(this).attr("l");
alert(l);
$.ajax({
type:"post",
url:"php_parsers/review.php",
data: $(l).serialize(),
success: function(response){
$("#reviewMain").html(response);
}
});
$.ajax({
type:"post",
url:"php_parsers/comment.php",
data: $(l).serialize(),
success: function(response){
$("#reviewSub").html(response);
}
});
});
});因此,在所有元素上设置了单击事件处理程序,类button_accept和$(this)将在单击时返回单击按钮。我希望这能解决你的问题。
顺便提一下,,您应该将l属性命名为data-l,以编写有效的html5代码。按钮元素没有属性l,命名空间data-*为您自己的(新的)属性命名。
https://stackoverflow.com/questions/32754903
复制相似问题