问题描述
我有一个 invoice.jsp
页,在这里我要计算在使用jQuery文本框中一定的价值。
在我的发票有一个量的文本框。如果用户输入,然后计算出的价格应该是动态的计算量,即(total_subPrice = UNIT_PRICE *量)
并在另一个文本框称为所示的价格。
现在我的电流输出是这样的:
我试过code以下,以解决这个问题,但我的jQuery的code是无法获得单价
文本字段的数据。我不知道为什么。请检查下面我code和建议我一个解决方案吧。
invoice.jsp
--------------------
--------------------
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(函数(){
$('输入[名字^ =数量]')。改变(函数(){
VAR单价= $(本).siblings('输入[名字^ =单价])VAL()。
警报(单价检查=+单价);
//在这一行的问题。无法获取单价
$(本).siblings('输入[名字^ =价格])VAL($(本).VAL()*单价)。
});
});
});
< / SCRIPT>
..............................
..............................
<! -
在这里,我通过一个列表,我dabase使用strus2框架标签迭代。
并确定在Struts2的标签比如我输入的字段值。 `< S:文本字段... />`
是一样的`<输入类型=文本... />
- >
<表格的宽度=100%高度=100%的边界=0的cellpadding =0CELLSPACING =0>
< S:迭代器值=#session.BOK状态=userStatus>
< TR风格=HEIGHT:10px的;>
< TD宽度=65%align =left>< S:属性值=bookTitile/>< / TD>
< TD宽度=10%align =left>
< S:文本字段名称=单价值=%{价格}大小=4/>
< / TD>
&所述; TD宽度=10%对准=中心>
< S:文本字段名称=数量值=%{}量大小=2/>
< / TD>
< TD宽度=15%对齐=中心>
< S:文本框值=%{价格}NAME =大小=6>< / S:文本框>
< / TD>
< / TR>
&所述; /秒:迭代>
< /表>
................................
................................
当我更改值在我的数量文本框,我的提醒框显示单价检查=不确定。请检查我的code和提出任何解决办法。
更新:生成的HTML
内部循环{
< TR风格=HEIGHT:10px的;>
< TD宽度=65%align =left>书标题显示和LT; / TD>
< TD宽度=10%align =left>
<输入类型=文本名称=单价值=%{价格}大小=4/>
< / TD>
&所述; TD宽度=10%对准=中心>
<输入类型=文本名称=数量值=%{}量大小=2/>
< / TD>
< TD宽度=15%对齐=中心>
<输入类型=文字值=%{价格}NAME =大小=6>< / S:文本框>
< / TD>
< / TR>
我得到它的工作: HTTP:/ /jsbin.com/efinak/2/edit
$(函数(){
$('输入[名称=数量]')。改变(函数(){
。VAR单价= $(本)。家长(TR)找到('输入[名称=单价])VAL()。
$(本)。家长(TR)找到('输入[名称=价格])VAL($(本).VAL()*单价)。
});
});
I have an invoice.jsp
page where I have to calculate some value in the textbox using jQuery.
In my invoice there is a quantity textbox. If the user enters the quantity then the calculated price should be calculated dynamically i.e (total_subPrice= unit_price * quantity)
and shown in another textbox called "price".
Now my current output is like this:
I tried the code below to solve this issue, but my jQuery code is unable to get the unitprice
textfield data. I don't know why. Please check my code below and suggest me a solution for it.
invoice.jsp
--------------------
--------------------
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$('input[name^="quantity"]').change(function() {
var unitprice = $(this).siblings('input[name^="unitprice"]').val();
alert("Unit price check="+unitprice);
//problem in this line. Unable to get the unit price
$(this).siblings('input[name^="price"]').val($(this).val() * unitprice);
});
});
});
</script>
..............................
..............................
<!--
Here I am iterating through a list from my dabase using strus2 framework tags.
And defined my input field values in struts2 tag eg. `<s:textfield.../>`
is the same as `<input type="text".../>
-->
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<s:iterator value="#session.BOK" status="userStatus">
<tr style="height: 10px;">
<td width="65%" align="left"><s:property value="bookTitile"/></td>
<td width="10%" align="left">
<s:textfield name="unitprice" value="%{price}" size="4"/>
</td>
<td width="10%" align="center">
<s:textfield name="quantity" value="%{quantity}" size="2" />
</td>
<td width="15%" align="center">
<s:textfield value="%{price}" name="" size="6"></s:textfield>
</td>
</tr>
</s:iterator>
</table>
................................
................................
When I change any value in my quantity textbox, my alert box is showing "Unit price check=undefined". Please check my code and suggest any solution.
Update: Generated html
inside loop {
<tr style="height: 10px;">
<td width="65%" align="left">book title display</td>
<td width="10%" align="left">
<input type="text" name="unitprice" value="%{price}" size="4"/>
</td>
<td width="10%" align="center">
<input type="text" name="quantity" value="%{quantity}" size="2" />
</td>
<td width="15%" align="center">
<input type="text" value="%{price}" name="" size="6"></s:textfield>
</td>
</tr>
I got it working: http://jsbin.com/efinak/2/edit
$(function() {
$('input[name="quantity"]').change(function() {
var unitprice = $(this).parents('tr').find('input[name="unitprice"]').val();
$(this).parents('tr').find(' input[name="price"]').val($(this).val() * unitprice);
});
});
这篇关于无法获取使用jQuery文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!