Javascript Jquery的每个函数都不是我所期望的那样工作
我正在尝试更改更改事件上的表值。当Javascript Jquery的每个函数都不是我所期望的那样工作,javascript,jquery,text,each,dom-manipulation,Javascript,Jquery,Text,Each,Dom Manipulation,我正在尝试更改更改事件上的表值。当.woocommerce variation点火时,新的比率通过多缸totalvaluefirst计算。然后使用totalvaluelast设置文本。但当触发第二个事件时,totalvaluefirst保持不变,文本不变 jQuery('.woocommerce-variation').bind("DOMSubtreeModified",function(){ jQuery( ".total-value" ).each(function(
.woocommerce variation
点火时,新的比率通过多缸totalvaluefirst
计算。然后使用totalvaluelast
设置文本。但当触发第二个事件时,totalvaluefirst
保持不变,文本不变
jQuery('.woocommerce-variation').bind("DOMSubtreeModified",function(){
jQuery( ".total-value" ).each(function() {
var totalvaluefirst = (jQuery( this ).text() );
var totalvaluefirstnew = ratio * totalvaluefirst;
var totalvaluelast = Math.round( totalvaluelast * Math.pow(10, 2) ) / Math.pow(10, 2);
jQuery( this ).text(totalvaluelast);
});
});
另见
我添加了一个保护措施:当您通过更改总值来修改dom时,它不应该导致重复处理dom事件
html:
.bind
不推荐使用,请改用.on
上的比率定义在哪里?此外,您的JS不完整,因为您缺少最后一个右括号()代码>我缩小了代码,它正在工作,但我的问题(jQuery(this).text())每次都给出相同的结果。您能告诉我们什么是jQuery(“.total value”)。长度
?您想做什么?我想有多种。变异元素。您要做的是为每个元素计算不同的值,并将其设置为每个元素的新值。这是可行的,但您似乎想做一些事情,比如合计账单,这就是您想要的吗?如何更改dom?更新你的小提琴!什么意思?我正在触发底部的ChangeDOM事件。不,我指的是类似于select元素的东西,以便OP可以检查它。
<div class="woocommerce-variation">
<div class="row">1
<div class="single-value">
11
</div>
<div class="total-value">
</div>
<div class="row">2
<div class="single-value">
20
</div>
<div class="total-value">
</div>
</div>
</div>
var ratio = 3.0;
var modifyingDOM = false;
jQuery('.woocommerce-variation').bind("DOMSubtreeModified",function(){
if(modifyingDOM)
return;
modifyingDOM = true;
jQuery(this).find( ".row" ).each(function() {
var totalvaluefirst = parseInt(jQuery( this ).find(".single-value").text() );
var totalvaluefirstnew = ratio * totalvaluefirst;
var totalvaluelast = Math.round( totalvaluefirstnew * Math.pow(10, 2) ) / Math.pow(10, 2);
jQuery( this ).find(".total-value").text(totalvaluelast);
});
modifyingDOM = false;
});
//change the DOM
jQuery('.woocommerce-variation').trigger("DOMSubtreeModified");