我正在研究一个项目,需要它必须是自动计算的。 假设我们有未计数的已知类和属性的隐藏输入。差异值,attr差异价格,div中要计算的price2价格3
我想做的是获取属性(价格,价格X2,价格X3) 如果用户插入了数字ex。 1或40,将返回第一个输入(price,priceX2,priceX3),如果给定61 0r 70,则将返回第三个输入(price,priceX2,priceX3),依此类推
<div id="countDiv">
<input type="number" value="" id="counter" />
<button id="countBtn"> Count </button>
<input type="hidden" value="40" price="1100" priceX2="1200" priceX3="1220" class="classeid">
<input type="hidden" value="60" price="1150" priceX2="1250" priceX3="1300" class="classeid">
<input type="hidden" value="70" price="1220" priceX2="1350" priceX3="1400" class="classeid">
</div>
<script>
$(document).ready(function(){
$("#countBtn").click(function(){
var parentDOM = document.getElementById("countDiv");
var classCounter = parentDOM.getElementsByClassName("classeid");
var counter = $("#counter").val();
for (var i = 0, n = classCounter.length; i < n; ++i) {
var mPrice = parseInt(classCounter[i].value);
var cPrice = parseInt(classCounter[i].getAttribute('price'));
var cPriceX2 = parseInt(classCounter[i].getAttribute('priceX2'));
var cPriceX3 = parseInt(classCounter[i].getAttribute('priceX3'));
}
});
});
</script>
非常感谢
答案 0 :(得分:0)
希望此代码对您有所帮助。 动态地执行此操作,如果您有3个以上的输入隐藏字段,则最好不要使用“隐藏”字段。在这种情况下,逻辑会有所不同。
仅考虑3个隐藏的输入字段,则代码如下:
HTML Code:
provide id to the each hidden input fields as first, second and third as written in the code.
JavaScript代码:
$("#countBtn").click(function(){
var counter = $("#counter").val();
if(counter > 0 && counter <= 40) {
var mprice = $("#first").val();
var cprice = $("#first").attr("price");
var cPriceX2 = $("#first").val("priceX2");
var cPriceX3 = $("#first").attr("priceX3");
}
else if(counter > 39 && counter <= 60) {
var mprice = $("#second").val();
var cprice = $("#second").attr("price");
var cPriceX2 = $("#second").val("priceX2");
var cPriceX3 = $("#second").attr("priceX3");
}
else {
var mprice = $("#third").val();
var cprice = $("#third").attr("price");
var cPriceX2 = $("#third").val("priceX2");
var cPriceX3 = $("#third").attr("priceX3");
}
}