简体   繁体   English

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

[英]getting value of dynamically created textbox using javascript

I have a order page, where customer selects product from a variety of categories then using a php foreach loop I am getting the selected products details from the database and show the selected products in step 2, where quantity is to be selected. 我有一个订单页面,客户在其中从各种类别中选择产品,然后使用php foreach循环从数据库中获取所选产品的详细信息,并在步骤2中显示所选产品,其中要选择数量。 In step two I need to calculate the subtotal of the product ie quantity * rate(fetched from db). 在第二步中,我需要计算产品的小计,即数量*费率(从db获取)。 I am unable to write the working javascript code for that. 我无法为此编写有效的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

The javascript code that I am using is: 我正在使用的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;
}

any help will be appreciated 任何帮助将不胜感激

You should try onchange event instead of onblur. 您应该尝试onchange事件而不是onblur。 And make sure your input rate and index, both of those are not null 并确保您输入的速率和索引都不为空

Try 尝试

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;
}

Note: Your vars quantity_field,target_field aren't objects - they are just strings which can be used to find the wanted object. 注意:您的varsquantity_field,target_field不是对象-它们只是可用于查找所需对象的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM