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

使用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

我正在使用codeigniter开发购物车。 在我的购物车页面中,我需要更新我的购物车,了解商品数量的变化。 我在点击按钮的帮助下得到了结果。 但我的问题是我需要在我的密钥上得到相同的结果。 对于按键,应单击相应按钮 p>

p>

查看页面 strong> p>

 &lt; td&gt; 
&lt;?php echo form_input(array('name'=&gt; $ items ['rowid']。“_ qty”,'  id'=&gt; $ items ['rowid']。“_ qty”,'value'=&gt; $ items ['qty'],'maxlength'=&gt;'3','size'=&gt;'5'  ));  ?&gt; 
&lt; button class =“btn btn-success”id =“&lt;?php echo $ items ['rowid']。”_ qty“?&gt;”  onclick =“update_cart('&lt;?= $ items ['rowid'];?&gt;')”&gt; update&lt; / button&gt; 
&lt; script&gt; 
 $(“#&lt;?php echo $ items  ['rowid']。“_ qty”?&gt;“)。keyup(函数(事件){
 
 $(”#&lt;?php echo $ items ['rowid']。“_ qty”?&gt;“  ).click(); 
 
}); 
&lt; / script&gt; 
&lt; / td&gt; 
  code>  pre> 
 
 

更新脚本 购物车 strong> p>

  function update_cart(itemid)
 {
 var qty = document.getElementById(itemid +“_ qty”)。value; 
 $ .ajax(  {
 type:'POST',
 url:'&lt;?php echo site_url(“ajax_controller1 / update_cart /'+ itemid +'/'+ qty +'”)?&gt;',
 data:{id:itemid  },
 success:function(response){
 $(“#shoppingcart_container”)。html(response); 
 $(“。total”)。click(); 
 
} 
});  
 
} 
  code>  pre> 
 
 

我只需要点击数量输入更改时的曲线索按钮 字段 这是我的演示网址我的网站链接 p> blockquote> div>

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

Check this

$("#id_of_field").keyup(function(event){

        $("#id_of_button").click();

});

You can simply add onkeyup property to your input element generation. Sorry, I'm not that fluent in php but you can use the same way you add other properties in: form_input(array( ..))

function update_cart(itemid, quantity)
{
  console.log(quantity);

}
<input type="text" onkeyup="update_cart('someItemId', this.value)" />

</div>

here is the code so your problem can be solved :

first you need to give a common class to input for getting value and id of input :

   $('.commonclass').on('input',function(e){
      var id = $(this).attr("id");    
      update_cart(id)
   });


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

     }
  });

} 

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

This is the easiset way

To call any event you only need to call trigger function and pass event name as parameter.

$(".total").trigger("click");