将ajax调用中的值返回到按下按钮的表中的特定行

时间:2015-02-02 17:21:17

标签: ajax

我是ajax / jquery的新手,我一直试图解决这个问题。我相信有一个简单的解决方案。我有一个由php / database生成的表,行的信息和数量可以改变。我试图通过按在同一行上的按钮将更新的值返回到行上的特定div。 我怎么能这样做?

HTML:

<div id="results"></div>

<table>
<tr>
   <td><div id=one>DATA1A</div></td>
   <td><div id=two>DATA1B</div></td>
   <td>A<input name="event1" class="eventcode" type="radio" value="A"> B<input name="event1" class="eventcode" type="radio" value="B"></td>
   <td><button type="button"  onclick="enter(this)" class="btn" data_id="Some other data 1">Enter</button></td>
</tr>
<tr>
   <td><div id=one>DATA2A</div></td>
   <td><div id=two>DATA2B</div></td>
   <td>A<input name="event2" class="eventcode" type="radio" value="A"> B<input name="event2" class="eventcode" type="radio" value="B"></td>
   <td><button type="button"  onclick="enter(this)" class="btn" data_id="Some other data 2">Enter</button></td>
</tr>
<tr>
   <td><div id=one>DATA3A</div></td>
   <td><div id=two>DATA3B</div></td>
   <td>A<input name="event3" class="eventcode" type="radio" value="A"> B<input name="event3" class="eventcode" type="radio" value="B"></td>       
   <td><button type="button"  onclick="enter(this)" class="btn" data_id="Some other data 3">Enter</button></td>
</tr>
</table>

AJAX:

$(document).on('click', '.btn', function() {
var variable1 = $(this).attr('data_id'); //this returns the right value for the selected row
var variable2 = $(this).closest('tr').find('.eventcode:checked').val(); //this returns the proper selected radio button for the selected row

$('#results') .html('<img src="loading.gif">'); //loading graphic

$.ajax({
    url: 'file to receive data',
    type : 'post',
    data : {variable1 : variable1 , variable2 : variable2},
    success: function(resp){
        $('#results').html($('#inner_1' , resp).html()); //this works to result DIV above table
        $('#one').html($('#inner_2' , resp).html()); //how do I return a value to the row/div id in which the button was pressed?
        $('#two').html($('#inner_3' , resp).html()); //how do I return a value to the row/div id in which the button was pressed?
    }
});
});

0 个答案:

没有答案