更改通过JSON传递的输入文本值

时间:2016-10-19 03:22:04

标签: php jquery mysql json ajax

我可以用一点帮助。我已经将我在PHP中创建的数组中的对象加载到一堆输入框中。对象用于购物车:价格,数量和总价格。虽然数据加载正常,但它似乎是只读的。

例如,数量的输入文本值为1,我无法更改它。

我希望能够更改数量,而无需返回我的MySQL表。

我可以使用常规Ajax做得很好,但是当我使用常规Ajax时,如果没有像我所有其他ajax函数一样刷新,项目将无法加载。

这是代码。

   function updates(){ 
         $.getJSON("classes/cartlist.php", function(data) {

         $("#cart_list, #cart_table").empty(); 
            $.each(data.result, function(){ $("#cart_list, #cart_table").append(
                   "<tr id='"+this['id']+"'> 
                         <td> "+this['pro_title']+" </td> 
                         <td> <input type='text' id='price-"+this["p_id"]+
                                "' class='price' pid='"+this["p_id"]+
                                "'value='"+this["price"]+"' /> 
                        </td>
                   </tr> "); 
                 }); 
            }); 

这不是一切,但另外两个输入框也是一样的。如果我这样离开它,我认为这会更容易阅读。

编辑:这是数量输入文本框的样子,因为有人问。

<td><input type='text' id='qty-"+this["p_id"]+"' class='qty' pid='"+this["p_id"]+"' value='"+this["qty"]+"' />

编辑:好的,所以我找到了无法更改文本的原因,那就是与JSON一起使用的设置间隔函数

  function done() {
            setTimeOut (
               function(){
                updates();
                }, 300)
             }

但如果我摆脱它,我将不得不刷新页面以恢复我的数据..

1 个答案:

答案 0 :(得分:0)

我认为追加字符串

时出错
function updates(){ 
     $.getJSON("classes/cartlist.php", function(data) {

     $("#cart_list, #cart_table").empty();
        //make use of index and value inside function 
        $.each(data.result, function(index,value){ 
            // here some thing with your code because \n indicate the end of statement so  
            $("#cart_list, #cart_table").append(
               "<tr id='"+this['id']+"'>" + 
                     "<td> "+this['pro_title']+" </td> "+
                     "<td> <input type='text' id='price-"+this["p_id"]+
                            "' class='price' pid='"+this["p_id"]+
                            "' value='"+this["price"]+"' />"+ 
                    "</td>"+
               "</tr> "); 
             }); 
        }); 
相关问题