动态相乘不同id的表行输入值
jQuery- Dynamically multiply input values of table rows with different id
使用下面的代码动态添加表行。为输入ID附加用户ID。
var selResId = jQuery('#grid').jqGrid('getGridParam', 'selarrrow');
var j=1;
for (var i=0, il=selResId.length; i < il; i++) {
var name = jQuery('#grid').jqGrid('getCell', selResId[i], 'USER_NAME');
$('#addr'+j).html("<td style='text-align:center;'>"+name+"</td><td><input id='hours_"+selResId[i]+"' value='80' type='text' readonly /></td><td><input id='rate_"+selResId[i]+"' type='text' /></td><td><input name='markup_"+selResId[i]+"' type='text'/></td><td><input name='totalcost_"+selResId[i]+"' type='text' readonly></td>");
$('#resource_table').append('<tr id="addr'+(j+1)+'"></tr>');
j++;
}
}
HTML生成<tr id="addr1">
<td>John Doe</td>
<td><input type="text" readonly="" value="80" id="hours_10"></td>
<td><input type="text" value="" id="rate_10"></td>
<td><input type="text" value="" id="markup_10"></td>
<td><input type="text" readonly="" value="" id="totalcost_10"></td>
</tr>
<tr id="addr2">
<td>Foo User</td>
<td><input type="text" readonly="" value="80" id="hours_11"></td>
<td><input type="text" value="" id="rate_11"></td>
<td><input type="text" value="" id="markup_11"></td>
<td><input type="text" readonly="" value="" id="totalcost_11"></td>
</tr>
我如何乘以hours, rate and markup
的输入值,并使用下面的公式将其显示在总成本投入下?事件可能是keyup
。
Initially, totalcost = hours * rate
Case 1: If markup (%) > 0, for eg: 10%, then markup_cost = (hours * rate * markup) / 100
totalcost = (hours * rate) + markup_cost
Case 2: If markup (%) < 0, for eg: -10%, then markup_cost = (hours * rate * markup) / 100
totalcost = (hours * rate) - markup_cost
尝试使用像
这样的选择器开头$(function(){
function setTotalCost(n){
var h=Number($('#hours_'+n).val()),
m=Number($('#markup_'+n).val()), // taking 0 if empty
r=Number($('#rate_'+n).val());
$('#totalcost_'+n).val(h*m*r);
}
$('[id^="rate_"]').on('keyup',function(){
var n = this.id.replace('rate_','');// get the number
setTotalCost(n);
});
$('[id^="markup_"]').on('keyup',function(){
var n = this.id.replace('markup_','');// get the number
setTotalCost(n);
});
});
$(function(){
function setTotalCost(n){
var h=Number($('#hours_'+n).val()),
m=Number($('#markup_'+n).val()), // taking 0 if empty
r=Number($('#rate_'+n).val());
$('#totalcost_'+n).val(h*m*r);
}
$('[id^="rate_"]').on('keyup',function(){
var n = this.id.replace('rate_','');// get the number
setTotalCost(n);
});
$('[id^="markup_"]').on('keyup',function(){
var n = this.id.replace('markup_','');// get the number
setTotalCost(n);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="addr1">
<td>John Doe</td>
<td>
<input type="text" readonly="" value="80" id="hours_10">
</td>
<td>
<input type="text" value="" id="rate_10">
</td>
<td>
<input type="text" value="" id="markup_10">
</td>
<td>
<input type="text" readonly="" value="" id="totalcost_10">
</td>
</tr>
<tr id="addr2">
<td>Foo User</td>
<td>
<input type="text" readonly="" value="80" id="hours_11">
</td>
<td>
<input type="text" value="" id="rate_11">
</td>
<td>
<input type="text" value="" id="markup_11">
</td>
<td>
<input type="text" readonly="" value="" id="totalcost_11">
</td>
</tr>
</table>
相关文章:
- <输入id=“;“到期”;type=“;月份;min=“;2016-05”>如何填写“;min”;属性与当前
- jquery如何检测当前输入id
- jQuery-如何从同一类中隐藏的输入id中获取不同的值
- 修改序列化表单函数以获取输入ID而不是名称
- 在innerHTML中包含变量值以创建输入id's和占位符
- 将输入id传递到jquery ui自动完成中的url
- 具有相同输入 ID 的多个表单仅使用 Ajax 发送第一个值
- 无法获取使用 event.target.id 触发事件的输入 ID
- 当输入id时,从json文件向警报添加一个图像
- 输入id以一个可变字符串开头
- 当用户在输入id=“”中填充文本时,如何调用函数java脚本;value_ 1”;然后按键盘上的“Enter”
- 如何连接函数参数和输入 ID
- 当我改变选择标记为什么不在输入id=“0”中显示数据时;消息”;
- 如何使用jquery将数字添加到输入ID名称中
- 使用值动态更改输入id-输入类型=范围
- Jquery插入表行并增加输入id
- 以 html 形式输入 ID,并在同一页面中从 MySQL 数据库加载相关数据
- 检测,自动将输入Id更改为当前Id名称
- 输入 id 时如何输出名称
- 从PHP调用JS函数时不能捕获输入ID