JavaScript 中无法从 div 获取值
我正在尝试根据预先计算的增值税金额来计算不含增值税的金额。 HTML 表单如下所示:
<table>
<tr>
<th>Sl No</th>
<th>Description of goods</th>
<th>Price</th>
<th>VAT%</th>
<th>Price with VAT</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
{foreach name = feach item = k from = $ip key = ind}
<tr>
<td>{$ind+1}</td>
<td>{$k->brand},{$k->model}</td>
<td id="prd_prc"> </td>
<td id="vat{$ind}">
<select name="vatpc" id="vatpc" onchange="calculate('{$ind}')">
<option value="5">5</option>
<option value="13.5">13.5</option>
</select>
</td>
<td id="total_cost{$ind}">{$k->price}</td>
<td>{$k->quantity}</td>
<td>{$k->total_cost}</td>
</tr>
{/foreach}
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>Total Cost </td>
<td>{$final_amount}</td>
</tr>
</table>
JavaScript 函数为:
function calculate(idno)
{
//alert(idno);
var vat_pc = document.getElementById("vatpc").value;
var total_cost = document.getElementById("total_cost"+idno).value;
//total_cost = Number(total_cost);
alert(total_cost);
//vat_pc = parseFloat(vat_pc);
// = v + 2.10 + 11.25;
//alert(v);
// document.getElementById("total").innerHTML = "Total - amount : "+v+" USD";
}
但警报显示 undefined
。我尝试添加 innerHTML
,但结果相同。请帮忙。提前致谢
I am trying to calculate amount without VAT from a pre-calculated amount with VAT . The HTML form looks like :
<table>
<tr>
<th>Sl No</th>
<th>Description of goods</th>
<th>Price</th>
<th>VAT%</th>
<th>Price with VAT</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
{foreach name = feach item = k from = $ip key = ind}
<tr>
<td>{$ind+1}</td>
<td>{$k->brand},{$k->model}</td>
<td id="prd_prc"> </td>
<td id="vat{$ind}">
<select name="vatpc" id="vatpc" onchange="calculate('{$ind}')">
<option value="5">5</option>
<option value="13.5">13.5</option>
</select>
</td>
<td id="total_cost{$ind}">{$k->price}</td>
<td>{$k->quantity}</td>
<td>{$k->total_cost}</td>
</tr>
{/foreach}
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>Total Cost </td>
<td>{$final_amount}</td>
</tr>
</table>
And the JavaScript function is :
function calculate(idno)
{
//alert(idno);
var vat_pc = document.getElementById("vatpc").value;
var total_cost = document.getElementById("total_cost"+idno).value;
//total_cost = Number(total_cost);
alert(total_cost);
//vat_pc = parseFloat(vat_pc);
// = v + 2.10 + 11.25;
//alert(v);
// document.getElementById("total").innerHTML = "Total - amount : "+v+" USD";
}
But the alert shows undefined
. I tried adding innerHTML
,but results same. Please help. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Total_costX 是一个
元素。使用 textContent 属性。
total_costX is a
<td>
element. Use the textContent property.没有什么比潜水更有价值的了。但你可以在 div 中获取文本或 html。如果你使用 jquery,它应该很简单:
或者 jQuery 的方式
$("#vatpc").text();
//或者
$("#vatpc").html();
There in nothing like value of dive. But you can get text or html inside dive. if you are using jquery, it should be quite simple:
Or jQuery's way
$("#vatpc").text();
//OR
$("#vatpc").html();