【发布时间】:2017-03-19 18:01:25
【问题描述】:
我有一个 jquery 应用程序,我需要根据表中存在的行数来评估表单中的变量。
期望的行为是找到产品的选定选项,提取值(然后对应于基于变量名称的预定义价格),然后对价格进行操作。我在评估变量名称、转换为数字、然后使用货币插件转换为货币时遇到问题。
这是表格:
<table id="sizetable" class="table table-responsive table-condensed">
<thead>
<tr>
<td width="30%">Shaft Type<input type="hidden" id="numfields" name="numfields" value="6"></td>
<td width="25%">Team Message (Free)<br><small>Appears on every shaft - 24 character max</small></td>
<td> </td>
<td width="25%">Individual Message<br><small>+$5 each - 24 character max</small></td>
<td> </td>
<td width="10%">Shaft Price</td>
<td width="10%"><button class="add_field_button btn btn-success">Add Shaft <span class="glyphicons glyphicons-circle-plus"></span></button></td>
</tr>
</thead>
<tbody>
<tr>
<td><select name="toproduct1" id="toproduct1" class="toproduct">
<option value="28">Team Order Specialty Attack Shaft - $25.00</option>
<option value="29">Team Order Specialty Defense Shaft - $50.00</option>
</select>
</td>
<td><input type="text" id="teammsg1" name="teammsg1" class="form-control" maxlength="24"></td>
<td><span class="badge tmsgchr">0</span></td>
<td><input type="text" id="indmsg1" name="indmsg1" class="form-control indmsg" maxlength="24"></td>
<td><span class="badge indmsgchr1"></span></td>
<td><span id="unitprice1"></span></td>
<td><a class="remove_field btn btn-danger" disabled>Remove <span class="glyphicons glyphicons-circle-minus"></span></a></td>
</tr>
<tr>
<td><select name="toproduct2" id="toproduct2" class="toproduct">
<option value="28">Team Order Specialty Attack Shaft - $25.00</option>
<option value="29">Team Order Specialty Defense Shaft - $50.00</option>
</select>
</td>
<td><span class="tmsg light-gray"></td>
<td></td>
<td><input type="text" id="indmsg2" name="indmsg2" class="form-control indmsg" maxlength="24"></td>
<td><span class="badge indmsgchr2"></span></td>
<td><span id="unitprice2"></span></td>
<td><a class="remove_field btn btn-danger" disabled>Remove <span class="glyphicons glyphicons-circle-minus"></span></a></td>
</tr>
...
我写的jquery如下:
$(document).ready(function() {
var max_fields = 20; //maximum input boxes allowed
var min_fields = 6; //minimum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var ordertot = 0;
var indbrandcost = 5;
var shaftprice = 0;
$(".toproduct, .indmsg").on('keyup keypress blur change paste cut', function(){
var id28 = 25; //dynamically written based on item price
var id29 = 50;
//loop through all fields, check if that row exists, then see which selection matches up with id28 or 1d29 above, then do something
for(i = 1; i < max_fields; i++) {
if($('#toproduct'+i).val() != '') {
var f = $('#toproduct'+i).find('option:selected').attr('value');
if($('#indmsg'+i).val() != '') {
$('#unitprice'+i).text((eval('id'+f)+5).currency()); //getting error here
//var shaftprice = shaftprice + indbrandcost;
}
else {
$('#unitprice'+i).text((eval('id'+f)).currency()); // getting error here
}
}
}
});
【问题讨论】:
-
与函数的 eval 部分对应的每个 js 控制台的错误:未捕获的 ReferenceError: idundefined is not defined at HTMLSelectElement.
(teamorders.cfm:1151) -
是的,那是因为它应该是 for(var i = 1; i
-
谢谢,这并没有解决错误。控制台给出了同样的信息。它不喜欢这一行: $('#unitprice'+i).text(eval('id'+f)+5).currency();它突出显示 eval 语句周围的 ()。