2016-02-27 85 views
0

設定值輸入我有這樣的HTML代碼,顯示一個表格爲什麼無法使用JavaScript

<table id="tabel-item-borrowed" class="table table-striped table-bordered table-hover"> 
 
    <thead> 
 
    <tr> 
 
     <th class="headtabel">Categories</th> 
 
     <th class="headtabel">Description</th> 
 
     <th class="headtabel">Action</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr id="t11"> 
 
     <td> 
 
     <input id="catinfo[1]" type="text" name="catinfo1" size="40"> 
 
     <input id="categories[1]" type="text" name="categories1" size="40"> 
 
     </td> 
 
     <td> 
 
     <input id="description[1]" type="text" name="description1" size="40"> 
 
     </td> 
 
     <td> 
 
     <input id="delete[1]" type="button" name="delete" value="delete" size="5"> 
 
     </td> 
 
    </tr> 
 
    <tr id="t12"> 
 
     <td> 
 
     <input id="catinfo[1]" type="text" name="catinfo1" size="40"> 
 
     <input id="categories[2]" type="text" name="categories2" size="40"> 
 
     </td> 
 
     <td> 
 
     <input id="description[2]" type="text" name="description2" size="40"> 
 
     </td> 
 
     <td> 
 
     <input id="delete[2]" type="button" name="delete" value="delete" size="5"> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

我想用JavaScript來充實內心<input id="catinfo[1]" type="text" name="catinfo1" size="40">值。這是我的代碼

,這JavaScript來填補PHP函數值轉換成HTML輸入

$("input#idposinfo").val(''); 
 
       $("input#categories\\[" + lastrow + "\\]").val(category); 
 
       $("input#description\\[" + lastrow + "\\]").val(description); 
 
       //succes fill the value inside catinfo 
 
       //$("input#catinfo\\[" + lastrow +"\\]").val(category); 
 

 
       $.post("get_category_info/" + $('#combocategory1').val() + "/" + $('#combocategory2').val(), {}, function (obj) { 
 
        console.log("get obj value"); 
 
        console.log(obj); //value in obj is int 25 
 
     $("input#catinfo\\["+lastrow+"\\]").val(obj); 
 
        console.log("success get obj value"); 
 
       }); 
 
       lastrow++; 
 
       $("input#lastrow").val(lastrow);

內$。員額中,$("input#catinfo\\[" + lastrow +"\\]").val(category);沒有工作

我如何填寫價值從$.post

+0

'lastrow'的價值是什麼?我在問這是因爲你在異步調用之外遞增它。 –

回答

0

因爲POST調用是異步的,從而成功獲得功能以後執行,但由於成功函數閉包你得到更新這導致了問題LASTROW值:

嘗試設置其未更新另一個局部變量:

var rowToUpdate = lastrow; 
$.post("get_category_info/" + $('#combocategory1').val() + "/" + $('#combocategory2').val(), {}, function (obj) { 
    $("input#catinfo\\["+rowToUpdate+"\\]").val(obj); 
    console.log("success get obj value"); 
}); 
相關問題