在jQuery方法中能调用JS方法吗?

在jQuery方法中能调用JS方法吗?

能,在jQuery方法中直接写入要调用已写好的方法名()即可。

在项目中,有多个地方用到结账的功能。
比如,
点击结账按钮,由JS获取对应行列的数据,进行计算并显示到页面,
点击改变数量按钮,由JS获取新的数据重新计算并显示,
点击移除按钮,又会用到上述JS方法。
这样导致JS文件中的代码行太多,而且都是重复的代码。
这时候,需要将上述重复用到的地方提取出来,写成一个方法。
让需要用到结算的地方去调用就可以了。
这里写了一个名为getResult的function.

function getResult() {

	var $input = $("input[class=count]");
	var $booktd = $("td[class=bookPrice]");
	var $disptd = $("td[class=disPrice]");
	var inputCount = $input.length;
	var bookPriceTotal = 0.00;
	var disPriceTotal = 0.00;
	var booksCount = 0;
	for (var i = 0; i < inputCount; i++) {
		var bookCount = $input.eq(i).val();
		var bookPrice = $booktd.eq(i).text();
		var disPrice = $disptd.eq(i).text();
		booksCount = booksCount + parseInt(bookCount);
		bookPriceTotal = bookPriceTotal + parseInt(bookCount) * bookPrice;
		disPriceTotal = disPriceTotal + bookCount * disPrice
	}
	var $totalMoney = $("td[class=totalMoney]");
	var $bookCount = $("td[class=bookCount]");
	var $saveMoney = $("span[class=saveMoney]");
	$totalMoney.text(disPriceTotal.toFixed(2) + " 元");
	$bookCount.text(booksCount + " 本");
	$saveMoney.text((bookPriceTotal - disPriceTotal).toFixed(2) + " 元");
}

在用到它的地方,可以直接调用名字()

//点击确定按钮时
$(function() {
	$("button[class=confirmBtn]").click(function() {
		getResult();  
	});
})
//点击移除某项时
$(function() {
	$("button[class='removesBtn']").click(function() {
		$("input[name='select']:checked").each(function() {
			n = $(this).parents("tr").index();
			$("table#myTable3").find("tr:eq(" + n + ")").remove();
		});
		getResult();
	});
	$("input[name='select']").attr("checked", false);

});

。。。。
都是在 jQuery中调用了JS方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值