为什么无法使用javascript设置值输入

时间:2016-02-27 18:35:47

标签: javascript jquery html

我有这个HTML代码,显示一个表

<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);

在$ .post内,$("input#catinfo\\[" + lastrow +"\\]" ).val(category);无效

如何填充$.post的价值?

1 个答案:

答案 0 :(得分: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");
});