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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?