【问题标题】:underfined input foreach php to jquery未定义的输入foreach php到jquery
【发布时间】:2013-05-23 11:17:46
【问题描述】:

我在输入中使用相同的名称循环 我希望用户禁用 javascript,该过程仍在进行

<?php 
$first = 1;
$id = array('id' => 'addcart');
foreach($data['info'] as $row):
    $first++;
    echo form_open('store/addcart/',$id); ?>
    <td width="11%" align="center"> 
        <span style="font-weight:bold;"><A href="<?=base_url().'store/products/'.$row['pcode'];?>" class="linkleft"><?=$row['pname'];?></a></span>
        <BR><SPAN class=pdesc><A href="<?=base_url().'store/products/'.$row['pcode'];?>"> Xem Chi tiết</a></SPAN>
        <BR>Giá: <span><?=$row['pprice'];?></span>
        <input type="hidden" name="id" value="<?=$row['pcode'];?>"/>
        <input type="hidden" name="qty" value="1"/>
        <input type="hidden" name="price" value="<?=$row['pprice'];?>"/>
        <input type="hidden" name="name" value="<?=$row['pname'];?>"/>
        <BR><INPUT type="submit" id="submit"><div id="loading"></div>
    </TD>
    <?php
    echo form_close();
    if($first == 4) {
        echo "</tr><tr>";
    }
endforeach;
?>

我发送到 jquery 来检查,但它未定义

<script type="text/javascript">
$(document).ready(function() { 
    $("form#addcart").submit(function() { 
        // Get the product ID and the quantity   
        var id = $(this).find('input[name=id]').val();
        var qty = $(this).find('input[name=qty]').val();

        alert('ID:' + id + '\n\rQTY:' + qty);  

        return false;
    });  

});  
</script>

如何在foreach中选中一个item?

【问题讨论】:

    标签: jquery foreach undefined


    【解决方案1】:

    for loop 中创建forms 时可以有多个表单

    所以将class 应用于每个form 就像 使用

    $id = array('id' => 'addcart','class'=>'addtocart');
    

    jQuery 中使用class namesubmit

    <script type="text/javascript">
    $(document).ready(function() { 
        $(document).on('submit',"form.addtocart",function(e) {//use class name in form
            e.preventDefault(); 
            // Get the product ID and the quantity   
            var id = $(this).find('input[name=id]').val();
            var qty = $(this).find('input[name=qty]').val();
            alert('ID:' + id + '\n\rQTY:' + qty);  
            return false;
        }); 
    });  
    </script>
    

    【讨论】:

    • 'class'=>'addtocart' 和 $("form.addtocart")。 JQuery 不工作,请帮助!取消定义:(
    • @chiviyeu 测试上面的答案我做了修改。
    • 谢谢,它重定向到 ...store/addcart,这意味着 jquery 不工作
    • @chiviyeu 上面的代码有错误我更正了,现在你可以再测试一次,也可以使用$(document).on('click',"form.addtocart #submit",function(){
    • 您是否添加了任何版本的jQuery
    猜你喜欢
    • 2011-05-25
    • 2018-09-14
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多