doubai9014
2017-01-26 23:52
浏览 71

在CodeIgniter中提交onChange事件的特定表单

I am working on POS web.

creating form for each item in cart/order i.e multiple forms in loop and giving them unique ids ('id'=>'cart_'.$line )(cart_1,cart_2). and created an update link in loop for each form. code below

echo form_open($controller_name."/edit_item/$line", array('class'=>'form-horizontal', 'id'=>'cart_'.$line));
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2', 'id'=>'quantity','class'=>'form-control'));
echo form_input(array('name'=>'discount','value'=>$item['discount'],'size'=>'3',  'id'=>'discount', 'class'=>'form-control'));?>
<a href="javascript:document.getElementById('<?php echo 'cart_'.$line ?>').submit();" id="anchor" title=<?php echo $this->lang->line('sales_update')?>  >

This fulfils the update requiremnt like when I update a quantity and click the link it updates the price.

But now the problem is that I want my form to submit on onChange event of quantity field.

1) First Try

<script type="text/javascript">
$("#quantity,#discount").on('change',function(){
var quantity=$("#quantity").val();
var discount=$("#discount").val();
if(quantity!=""&&discount!=""){
document.getElementById('anchor').click();
console.log('form send');
}
});
</script>

this is what I tired but it only works if there is only one item in order

2)Second try

function updateQuantity(anchorID){
if(anchorID != ""){
document.getElementById(anchorID).click();
}
}
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2', 'onChange'=>'updateQuantity(HERE I WANT TO PASS "anchorID_LOOP VALUE")' 'id'=>'quantity','class'=>'form-control'));
<a href="javascript:document.getElementById('<?php echo 'cart_'.$line ?>').submit();" id='<?php echo 'anchorID_'.$line ?>' title=<?php echo $this->lang->line('sales_update')?>  >

图片转代码服务由CSDN问答提供 功能建议

我正在开发POS网站。

为每个项目创建表单 购物车/订单,即循环中的多个表单,并为它们提供唯一的ID('id'=&gt;'cart _'。$ line)(cart_1,cart_2)。 并在循环中为每个表单创建更新链接。 下面的代码

  echo form_open($ controller_name。“/ edit_item / $ line”,array('class'=&gt;'form-horizo​​ntal','id'=&gt;  'cart _'。$ line)); 
echo form_input(array('name'=&gt;'quantity','value'=&gt; $ item ['quantity'],'size'=&gt;'2','  id'=&gt;'数量','class'=&gt;'form-control')); 
echo form_input(array('name'=&gt;'discount','value'=&gt; $ item ['折扣 '','size'=&gt;'3','id'=&gt;'折扣','class'=&gt;'form-control'));?&gt; 
&lt; a href =“javascript:document  .getElementById('&lt;?php echo'cart _'。$ line?&gt;')。submit();“  id =“anchor”title =&lt;?php echo $ this-&gt; lang-&gt; line('sales_update')?&gt;  &gt; 
   
 
 

这符合更新要求,就像更新数量并点击更新价格的链接一样。

但现在的问题是我希望我的表单在数量字段的onChange事件上提交。

1)首先尝试 \ n

 &lt; script type =“text / javascript”&gt; 
 $(“#quantity,#discount”)。on('change',function(){
var quantity = $(  “#quantity”)。val(); 
var discount = $(“#discount”)。val(); 
if(quantity!=“”&amp;&amp; discount!=“”){
document.getElementById(  'anchor')。click(); 
console.log('form send'); 
} 
}); 
&lt; / script&gt; 
   
 
 

这就是我累了,但只有按顺序只有一个项目才有效

2)第二次尝试

  function updateQuantity(anchorID){
if(anchorID!=“”){
document.getElementById(anchorID).click(); 
} 
} 
echo form_input(array('name  '=&gt;'数量','价值'=&gt; $ item ['数量'],'尺寸'=&gt;'2','onChange'=&gt;'updateQuantity(HERE 我想通过“anchorID_LOOP VALUE”)''id'=&gt;'quantity','class'=&gt;'form-control')); 
&lt; a href =“javascript:document.getElementById('&lt;  ?php echo'car _'。$ line?&gt;')。submit();“  id ='&lt;?php echo'assonerID _'。$ line?&gt;'  title =&lt;?php echo $ this-&gt; lang-&gt; line('sales_update')?&gt;  &gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douleng3463 2017-01-27 00:50
    已采纳

    Rather than triggering a button.click() You should try the following:

    echo form_open($controller_name."/edit_item/$line", array('class'=>'form-horizontal line-item', 'id'=>'cart_'.$line));
    echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2', 'id'=>'quantity','class'=>'form-control cartline', 'data-form' => $line));
    echo form_input(array('name'=>'discount','value'=>$item['discount'],'size'=>'3',  'id'=>'discount', 'class'=>'form-control cartline', 'data-form' => $line));?>
    <a href="javascript:document.getElementById('<?php echo 'cart_'.$line ?>').submit();" id="anchor" title=<?php echo $this->lang->line('sales_update')?>  >
    

    Notice I gave the form control an extra class and a data- attribute to hold the $line variable

    So now I can catch the event and submit the form

    $(function(){
        $('.cartline').change(function(){
            var line = $(this).attr('data-form');
            $('#cart_' + line).submit();
        });
    });
    

    To send the form via AJAX, you have to handle the form submit function (I gave the form a new class line-item)

    $(".line-item").submit(function(event) {   
          event.preventDefault();
          var line_form = $( this ),
              url = line_form.attr( 'action' );
          //Make your data 
          $.post( url, { data-field1: $('text1').val(), data-field1: $('text2').val() }, function(data){ 
             alert('Form Posted') 
          });
    
    
        });
    
    打赏 评论

相关推荐 更多相似问题