使用javascript获取动态创建的文本框的值


getting value of dynamically created textbox using javascript

我有一个订单页面,客户在其中从各种类别中选择产品,然后使用phpforeach循环,我从数据库中获取所选产品的详细信息,并在步骤2中显示所选产品,其中要选择数量。在第二步中,我需要计算产品的小计,即数量*费率(从数据库中提取)。我无法为此编写可工作的javascript代码。

<input type="text"  name="qty<?php echo $index; ?>" maxlength="7" class="bo-text" onblur = "getnsetvalue(<?php echo $sp; ?>, <?php echo $index; ?>);"> // the textbox where quantity is added
<input type="text"  readonly name="subtotal<?php echo $index; ?>" maxlength="7" class="bo-text"> //the texbox where subtotal will be computed and displayed

我使用的javascript代码是:

function getnsetvalue(rate, index){
        var quantity_field = "qty" + index;
        var quantity = document.product2.quantity_field.value;      
        var stotal = parseFloat(rate*quantity);
        var target_field = "subtotal" + index;
        document.product2.target_field.value=stotal;
}

任何帮助都将不胜感激

尝试

function getnsetvalue(rate, index){
    var quantity_field = "qty" + index;
    var quantity = document.getElementsByName(quantity_field)[0].value      
    var stotal = parseFloat(rate*quantity);
    var target_field = "subtotal" + index;
    document.getElementsByName(target_field)[0].value=stotal;
}

注意:vars quantity_field和target_field不是对象,它们只是可以用来查找所需对象的字符串。

您应该尝试onchange事件,而不是onblur。并确保您的输入速率和索引,两者都不是空