javascript - How to add input field using jQuery and call Ajax into new input field -
i using table input fields. use jquery adding new row/ input field, , want call ajax new row/input field. not working. because it's not filling condition of document.ready()
function..
here html form:
<table> <thead> <tr> <th>account name:</th> <th>branch:</th> </tr> </thead> <tr> <td> <div> <input type="text" name="ac_name" class="auto-search-ac"> </div> </td> <td> <div> <input type="text" name="branch"> </div> </td> </table>
script add new row in table ( working perfectly) :
<script> $(document).on("focus",'#table tr:last-child td:last-child',function() { //append new row here. var table = $("#table"); table.append('<tr>\ <td style="width:250px;"><div> <input type="text" name="ac_name" class="auto-search-ac"></div>\ </td>\ <td><div><input type="text" name="branch"></div>\ </td>\ </tr>'); }); </script>
ajax call new inserted input field:: (in first row - ajax working nicely)
<script type="text/javascript"> $(".auto-search-ac").autocomplete({ source: "/ca-list", minlength: 1, select: function( event, ui ) { $('.auto-search-ac').val(ui.item.value); $('#ca-id-val').val(ui.item.ca_id); } }); </script>
note :: using scripts , html in modal. in first row, ok.
after adding new row jquery can not call ajax. may issue of document.ready()
.
question : how may call script/ajax/jquery after adding new input field/row using jquery in html ? in advanced.
use class selector not have element having auto-search-ac
. after appending element, find element having class auto-search-ac
appended tr
, initialize autocomplete
try this:
$(".auto-search-ac").autocomplete({ source: "/ca-list", minlength: 1, select: function(event, ui) { alert(ui.item.value); alert(ui.item.ca_id); } }); $(document).on("focus", '#table tr:last-child td:last-child', function() { var table = $("#table"); var element = '<tr>\ <td style="width:250px;"><div> <input type="text" name="ac_name" class="auto-search-ac"></div>\ </td>\ <td><div><input type="text" name="branch"></div>\ </td>\ </tr>'; table.append(element); $("#table tr:last-child").find(".auto-search-ac").autocomplete({ source: "/ca-list", minlength: 1, select: function(event, ui) { alert(ui.item.value); alert(ui.item.ca_id); } }); });
<table id='table'> <thead> <tr> <th>account name:</th> <th>branch:</th> </tr> </thead> <tr> <td> <div> <input type="text" name="ac_name" class="auto-search-ac"> </div> </td> <td> <div> <input type="text" name="branch"> </div> </td> </table>
Comments
Post a Comment