查找每个带有onchange js的文本框

foreach textboxes with onchange js on it

本文关键字:js 文本 onchange 查找      更新时间:2023-09-26

我试图在我拥有的每个文本框上都有一个onchange事件。但我的脚本只适用于第一行的文本框。有人能帮我吗?

            <?php
        foreach($get_order_details as $row_order_details):
        ?>
    <form method = "post">
        <tr>
            <td width="180px"><?php echo $row_order_details['name']; ?></td>
            <td width="180px"><?php echo $row_order_details['description']; ?></td>
            <td width="180px"><?php echo $row_order_details['qty']; ?></td>
            <td width="145px"><input type = "text"  name = "received_qty" id = "received_qty" class = "form-control" onchange = "myFunction1();"/></td>
            <td width = "145px"><input class = "form-control" type = "text" name = "cost" id = "cost" onchange = "myFunction();" /></td>
            <th width = "145px"><input class = "form-control" type = "text" name = "total_cost" id = "total_cost" disabled /></th>
            <td><button type = "submit" value = "" name = "received" class = "btn btn-primary">Receive</button></td>
        </tr>
    </form>
        <?php
        endforeach;
        ?>

这是我的javascript代码。

这是我的脚本

            <script>
    function myFunction1()
    {
        var received_qty = document.getElementById('received_qty').value;
        var cost = document.getElementById('cost').value;
        var total = received_qty * cost;    
        var cost = document.getElementById('total_cost').value = total;
    }
    function myFunction()
    {
        var received_qty = document.getElementById('received_qty').value;
        var cost = document.getElementById('cost').value;
        var total = received_qty * cost;
        var cost = document.getElementById('total_cost').value = total;
    }
    </script>

事实上,你写了一些关于jquery将为您提供这个解决方案:)

JSFIDDLE

一些输入

<input type="text" class="foo" id="i1" />
<input type="text" class="foo" id="i2" />
<input type="text" class="foo" id="i3" />
<input type="text" class="foo" id="i4" />
<input type="text" class="foo" id="i5" />
<input type="text" id="i6" />
<input type="text" id="i7" />
<input type="text" id="i8" />
<input type="text" id="i9" />
你jquery

$(document).ready(function(){
    $('.foo').on('change', function(){
       alert($(this).val()); 
    });
});