dongluo8439 2017-09-16 07:26
浏览 58
已采纳

使用jquery或javascript单击按键上的按钮

I am developing a shopping cart using codeigniter. In my cart page I need to update my cart on change in quantity of the item. I am getting the results with the help of button click. But my issue is that I need to get the same result on my key up. For that on key up corresponding button should be clicked

Cart page Image

View page

    <td>
     <?php echo form_input(array('name' => $items['rowid']."_qty",'id' => $items['rowid']."_qty", 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5'));  ?>
<button class="btn btn-success " id= "<?php echo $items['rowid']."_qty" ?>" onclick="update_cart('<?=$items['rowid'];?>')" >update</button>
 <script>
  $("#<?php echo $items['rowid']."_qty" ?>").keyup(function(event){

     $("#<?php echo $items['rowid']."_qty" ?>").click();

      });
       </script>
    </td>

Script for update cart

function update_cart(itemid)
{ 
  var qty= document.getElementById(itemid+"_qty").value;
        $.ajax({
            type: 'POST',
            url: '<?php echo site_url("ajax_controller1/update_cart/'+itemid+'/'+qty+'")?>',
            data: { id:itemid }, 
            success:function(response){
              $("#shoppingcart_container").html(response);
$(".total").click();

     }
  });

} 

I just need to click currosponding button on change in quantity input field This is my Demo URL My website link

  • 写回答

6条回答 默认 最新

  • douyun3631 2017-09-16 07:34
    关注

    No need to add extra code to trigger click event. You can directly put update_cart with your form_input as

    <?php 
        echo form_input(array(
                'name' => $items['rowid']."_qty",
                'id' => $items['rowid']."_qty", 
                'value' => $items['qty'], 
                'maxlength' => '3', 
                'size' => '5',
                'onkeyup' => "update_cart('".$items['rowid']."')"
            )
        );  
    ?>
    

    Replace <td> tag with above code and try

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable