javascript - How can I pass the row id from a table with a button -
i have table want add button each row , edit row in form. need pass linenumber getjson request. how can that? here table , getjson. (edit)i noticed i'm asking linenumber want id of button pushed '+value.linenumber+'.
//table goes though loop. table_obj.append($('<tr><td>'+value.sln+'</td><td >'+value.type+'</td><td > '+value.stopnumber+'</td><td>'+value.loname+'</td><td >'+value.referenceno+'</td> <td class="hide" >'+value.typeid+'</td><td class="hide" >'+value.locationid+'</td> <td class="hide" >'+value.linenumber+'</td><td><button id='+value.linenumber+' onclick=edit()>edit</button></td></tr>')); //getjson <script> function edit() $.getjson("editstops.php",{ linenumber:$('#linenumber').val() },stophandler) var stophandler = function(data){ $("#slns").val(data[0].value.loadnumber); $("#type").val(data[0].value.type); $("#stopnumber").val(data[0].value.stopnumber); $("#loname").val(data[0].value.loname); $("#referenceno").val(data[0].value.referenceno); $("#typeid").val(data[0].value.typeid); $("#locationid").val(data[0].value.locationid); $("#linenumber").val(data[0].value.linenumber); }; </script>
in button tag, have line number specified id attribute. can pass edit()
function argument:
<button id='+value.linenumber+' onclick="edit('+value.linenumber+')">edit</button></td></tr>
then edit function can accept argument , use it:
function edit(linenumber) { $.getjson("editstops.php",{ linenumber: linenumber }, stophandler); }
note if buttons inside form
tag might need add return false;
after calling edit()
prevent submitting form , refreshing page.
simple working example: http://jsbin.com/okesom/2/edit
alternatively leave off onclick
event in button tag entirely. after loop done , table built, attach event buttons this:
$("button").click(function() { var linenumber = $(this).attr('id'); $.getjson("editstops.php",{ linenumber: linenumber }, stophandler); });
here quick example of second option: http://jsbin.com/ibokon/2/edit
Comments
Post a Comment