0
我已经使用了来自http://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm的优秀jQuery计算插件。但是我需要对它进行编辑才能在CMS表单模块中工作,这需要jQuery将我想要的值直接写入到在表单上动态创建的html只读输入标记中。计算预订表总计
因此,而不是由上述计算插件提供的默认html代码,该代码非常适用于开箱即用。我需要写一个jQuery这需要价格项目,将输出发送到填充的价格标签和总的标签 - 即
$("input[id$='childTotal']").val('25.99')
低于我的HTML代码示例:
<td><TextBox name="childNumber" Id="childNumber" DataField="ChildPassengers" DataType="int32" /></td>
下面是我的jQuery代码(这是行不通的)
<script type="text/javascript">
var bIsFirebugReady = (!!window.console && !!window.console.log);
$(document).ready(
function(){
// update the plug-in version
$("#idPluginVersion").text($.Calculation.version);
// bind the recalc function to the input adult quantity fields
$("input[name^=adultNumber]").bind("keyup", recalc);
// run the calculation function now
recalc();
// bind the recalc function to the input child quantity fields
$("input[name^=childNumber]").bind("keyup", recalc);
// run the calculation function now
recalc();
$("input[name^=sum]").sum("keyup", "#totalSum");
// automatically update the "#totalAvg" field every time
// the values are changes via the keyup event
$("input[name^=avg]").avg({
bind:"keyup"
, selector: "#totalAvg"
// if an invalid character is found, change the background color
, onParseError: function(){
this.css("backgroundColor", "#cc0000")
}
// if the error has been cleared, reset the bgcolor
, onParseClear: function(){
this.css("backgroundColor", "");
}
});
// automatically update the "#minNumber" field every time
// the values are changes via the keyup event
$("input[name^=min]").min("keyup", "#numberMin");
// automatically update the "#minNumber" field every time
// the values are changes via the keyup event
$("input[name^=max]").max("keyup", {
selector: "#numberMax"
, oncalc: function (value, options){
// you can use this to format the value
$(options.selector).val(value);
}
});
// this calculates the sum for some text nodes
$("#idTotalTextSum").click(
function(){
// get the sum of the elements
var sum = $(".textSum").sum();
// update the total
$("#totalTextSum").text("$" + sum.toString());
}
);
// this calculates the average for some text nodes
$("#idTotalTextAvg").click(
function(){
// get the average of the elements
var avg = $(".textAvg").avg();
// update the total
$("#totalTextAvg").text(avg.toString());
}
);
}
);
function recalc(){
$("[id$=adultTotal]").calc(
// the equation to use for the calculation
"Aqty * Aprice",
// define the variables used in the equation, these can be a jQuery object
{
Aqty: $("input[name$=adultNumber]"),
Aprice: $("input[id$='adultPrice']").val('50.99')
},
var Atotal=Aqty * Aprice;
Atotal.toFixed(2); // two decimal places
$('#adultTotal').html(Atotal.toFixed(2));
function recalc(){
$("[id$=childTotal]").calc(
// the equation to use for the calculation
"Cqty * Cprice =Csum",
{
Cqty: $("input[name$=childNumber]"),
Cprice: $("input[id$='childPrice']").val('25.99')
},
var Ctotal=Cqty * Cprice;
Ctotal.toFixed(2); // two decimal places
$('#childTotal').html(Ctotal.toFixed(2));
function recalc(){
$("[id$=grandTotal]").calc(
var Grantotal=Atotal + Ctotal;
Grantotal.toFixed(2); // two decimal places
$('#grandTotal').html(Gtotal.toFixed(2));
}
);
}
</script>
这是一个代码_lot_,你需要用_much_更详细地描述你的意思是“不起作用”。你可以缩小到失败的函数调用或行吗? – 2011-03-06 22:24:32
尽管我喜欢jQuery插件,但我不会使用基本算术。使用少量的JavaScript和jQuery选择器,在JavaScript中进行基本算术不会有任何问题。如果你不必这么做,我可能会花更多时间学习JavaScript的基础知识。 – 2011-03-06 22:36:13