有人可以向我解释为什么这有效吗:
function updateWellnessPrice() {
$('#toggle_151, #toggle_170').click(function () {
amount = '1.50';
$('#toggle_153 input').attr('data-amount', amount);
$('#toggle_153 span').html(amount);
});
$('#toggle_148, #toggle_149, #toggle_150, #toggle_167, #toggle_168, #toggle_169').click(function () {
amount = '0';
$('#toggle_153 input').attr('data-amount', amount);
$('#toggle_153 span').html(amount);
});
};
但是我不喜欢这样的代码,我想以简写的形式编写它。为什么下面的这段代码不起作用?
$('#toggle_151, #toggle_170').click(function () {
var $this = $(this);
if($this.data('clicked') == true) {
amount = '1.50';
} else {
console.log('check');
amount = '0';
}
$('#toggle_153 input').attr('data-amount', amount);
$('#toggle_153 span').html(amount);
});
甚至控制台日志也没有显示出来,因此他不喜欢该单击功能中的else。
不知道这里的if
条件是否有意义
试试这个
$('#toggle_148, #toggle_149, #toggle_150, #toggle_167, #toggle_168, #toggle_169, #toggle_151, #toggle_170').click(function () {
var currentId = $(this).attr( "id" );
if( currentId == "toggle_151" || currentId == "toggle_170" )
{
amount = '1.50';
}
else
{
console.log('check');
amount = '0';
}
$('#toggle_153 input').attr('data-amount', amount);
$('#toggle_153 span').html(amount);
});
现在,假设您要为所有id编写一个单击处理程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句