我是JavaScript的新手,正在尝试编写一些代码,在一个表单中列出三个价格选项,如复选框。如果前两个都被选中,我希望总价格下降一定的量。
我的代码基于这个问题中的代码:Javascript checkboxes incorrect calculating
它们通过日期变量来重置基值。我假设,如果我有一个函数,在选中这两个框的情况下将基数设置为负值,这将实现我想要的结果。当这种情况发生时,我还希望输出有一个额外的标签'save (x) per month‘。
但是,我不确定如何将变量与复选框关联起来。我需要按照how to change the value of a variable based on a select option:selected使用jquery吗?
发布于 2011-08-01 07:42:12
我假设您正在修改前面问题中的recalculate()函数。在该函数中,在获得总和后,添加以下内容以应用折扣:
if ($("#chkBox1,#chkBox2").filter(":checked").length == 2)
{
sum -= discount;
}要将消息写入输出,请将$("#output").html(sum)代码修改为:
$("#output").html(sum + " Save $" + discount + " per month");发布于 2011-08-01 07:26:53
Jquery从来都不是必需的,但它始终是一个优势。既然你正在学习javascript,我建议你暂时不要使用这个框架。
首先,您需要一个表单(如果您向我们展示您的表单会更好):
<form>
<input type="checkbox" id="first" /><label for="first">First Box</label> <br>
<input type="checkbox" id="second" /><label for="second">First Box</label> <br>
<input type="text" id="output" value="5.00"/>
</form>现在是javascript
<script type="text/javascript">
var first = document.getElementById("first");
var second = document.getElementById("second");
first.onchange = function () {
if (this.checked == true) {
document.getElementById("output").value = "new Value";
}else {
document.getElementById("output").value = "other Value";
}
}
... the same can be done for 'second' ....
</script>发布于 2011-08-01 07:18:56
if (document.getElementById('checkBox1').checked && document.getElementById('checkBox2').checked) {
// Change the price
}https://stackoverflow.com/questions/6892935
复制相似问题