Ajax :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
var insPer =$("#insPer").val();
var insPos =$("#insPos").val();
$.ajax({
url:'../validate/inspire.php',
method:'POST',
data:{
u_id:insPer,
p_id:insPos
},
success:function(data){
//alert(data);
}
});
});
});
</script>
这是我的输入和按钮
<span class="views" data-toggle="tooltip" title="Inspired">
<input type="hidden" id="insPer<?php echo $p_id; ?>" name="insPer">
<input type="hidden" id="insPos<?php echo $p_id; ?>" name="insPos">
<a href="#" role="button" id="button<?php echo $p_id; ?>" type="submit">
<img src="../images/lightbulb-regular.svg" class="like-btn-svg">
</a>
</span>
此处输入ID来自循环每个帖子。 id值的变化像 示例:
loop1 {insPer1,insPos1,button1},
loop2 {insPer2,insPos2,button2},
我正在ajax中传递这些ID,但是ID值没有改变。
答案 0 :(得分:1)
尝试使用value
属性设置隐藏元素的值...
<span class="views" data-toggle="tooltip" title="Inspired">
<input type="hidden" id="insPer" name="insPer" value="<?php echo $p_id; ?>" />
<input type="hidden" id="insPos" name="insPos" value="<?php echo $p_id; ?>" /> <-- assuming $p_id should be the value of this input element -->
<a href="#" role="button" id="button" type="submit">
<img src="../images/lightbulb-regular.svg" class="like-btn-svg">
</a>
</span>