如果我使用以下代码进行计算并将其分配给变量,我将如何使该变量可在html页面上的任何位置访问?我以为只是声明它,result from elsewhere on the page = (quantity * product).toFixed(2) ;
然后允许我使用将其输出到控制台,console.log('result from elsewhere on the page is p * q ' + result);
但是当我这样做时,我得到result from elsewhere on the page is p * q [object HTMLSpanElement]
了输出
<script>
$('#quantity,#product').bind('change keyup', function () {
var product = $('#product').val(),
quantity = parseInt($('#quantity').val(), 10),
result = (quantity * product).toFixed(2);
$('#result').text(result);
console.log('Testing console');
console.log('result is p * q ' + result);
console.log('quantity is ' + quantity);
});
</script>
注意:第一个输出console.log('result is p * q ' + result);
工作正常,result is p * q 98.00
仅当我从外部尝试result from elsewhere on the page is p * q [object HTMLSpanElement]
返回时,才输出。
感谢您对此的任何帮助。
使用window.result
或window["result"]
符号。
编辑:我以为你没有改变result
其他地方。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句