我有一個使用while循環創建的表格,它顯示項目列表,以便用戶可以選擇要通過ajax完成的項目刪除。通過ajax傳遞最接近的值
我想使用JQuery's
最接近的屬性,但我不能找出正確的語法。我從這個得到的輸出是Object Object
表
<form id="del_opinion">
<table id="asked_list">
<?php
while($row = mysqli_fetch_assoc($result)){
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<input type="button" name="del_question" value="" class="trash_btn">
<input type="hidden" class="id_question" name="question_id" value="<? echo $row['questionID']; ?>">
<input type="hidden" class="id_user" name="user" value="<?php echo $id; ?>" >
</td>
</tr>
<? } ?>
</table>
</form>
阿賈克斯
<script>
$(document).ready(function() {
$('.trash_btn').click(function(){
var question = $(this).closest('.id_question');
var user = $(this).closest('id_user');
$.ajax({
url: 'opinion_del.php',
type:'POST',
data: 'question='+ question +'&user='+user,
dataType: 'json',
success: function(response){
$('#responses_table').html(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
} // End of success function of ajax form
}); // End of ajax call
});
});
</script>
的PHP已經用於測試
$question_id = $_POST['question'];
$id = $_POST['user'];
$sql = "SELECT * FROM adviceQuestion WHERE questionID = '$question_id'";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
if($result){
$output = $question_id;
}
echo json_encode($output);
做。如果我鍵入一個文本值$output
它工作正常,如果我輸入一個文字值他們正常工作過,所以我相信這是值得做的最接近的語法
我也嘗試添加一個類叫做details
和錶行使用
var question = $(event.target).closest('.details').find('.id_question');
如果我警惕的問題是說[object Object]
嘗試刪除數據類型: 'Json的' 因爲: echo json_encode($ output);你輸出和ID所以它是字符串或嘗試輸出數組... 我有類似的記不得但你的代碼看起來很好,我... – Froxz
它仍然是同樣的事情,只是說[對象對象] – tatty27
如果我鍵入一個文字值轉換成'var user',該值被正確地傳回 – tatty27