【发布时间】:2012-03-06 13:35:45
【问题描述】:
我的问题很简单,不像看起来那么冗长。
我有 7 个文本框,其 id 为 text1、text2、text3、text4 和 text5、text6、text7,以及一个具有 id 检查的复选框。我通过 JSON 获得 text1 和 text2 的值为 10 和 20,并在 text7 中显示总计 30。 text4、text5 和 text6 的值为空,即这些文本框显示“0”。 text3 和 text4 将根据复选框自动填充。
当我选中复选框时,“点”将在 text3 中自动填充,“100”将在 text4 中自动填充。现在总 text7 将显示 130。 text5 和 text6 的值显示“0”。如果我要写一些东西,让我们在 text5 中说“10”,在 text6 中说“20”,那么总 text7 将显示 160。如果我取消选中该复选框,那么 text3 和 text4 的值将被删除,并且总将相应地改变。我不是选中复选框后能够知道如何自动填充 text3 和 text4,请问有什么想法吗?
index.jsp
$(document).ready(function() {
$("#combo1").change(function() {
$.getJSON(
'combo1.jsp',
{ combo1Val : $(this).val() },
function(data) {
var a = data.valueoffirsttextbox; //Got 10 from db
var b = data.valueofsecondtextbox; //Got 20 from db
var total = parseInt(a) + parseInt(b);
$("#combo2").val(data.name);
$("#text1").val(a)
$("#text2").val(b)
$("#text7").val(total); // here i am showing total 30 in text7
}
);
// when checkbox is unchecked text4 is not present
$("#text1, #text2, #text5, #text6").keyup(function() {// if any editing occurs
in these values then total will be changed accordingly
var a = $("#text1").val();
var b = $("#text2").val();
var c = $("#text5").val();
var d = $("#text6").val();
var total = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d);
$("#text7").val(total);// shows total with out text4's value
// when checkbox is checked then text4's value is added
$("#text1, #text2, #text4, #text5, #text6").keyup(function() {// if any editing
occurs in these values then total will be changed accordingly
var a = $("#text1").val();
var b = $("#text2").val();
var c = $("#text5").val();
var d = $("#text6").val();
//here text4 is added
var e = $("#text4").val();
var total = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d) + parseInt(e) ;
$("#text7").val(total);// show total with text4's value
});
});
});
<body>
<input type="checkbox" id=check"/>
<input type="text" id="text1"/>
<input type="text" id="text2"/>
<input type="text" id="text3"/>// how can i autofill "POINTS" in this
textbox after checking the checkbox?
<input type="text" id="text4" value="0"/>//how can i autofill "100" in this textbox
after checking the checkbox?
<input type="text" id="text5" value="0"/>
<input type="text" id="text6" value="0"/>
<input type="text" id="text7" value="0"/>// shows total of all values of above
textboxes except text3
</body>
【问题讨论】:
-
你能创建它的jsFiddle吗?
-
您必须在 text3、4 中显示的值并同样修复吗?
-
@Logan..yes text4 的值固定为 100
-
@AmarPalsapure..我正在尝试小提琴,请给我一个想法
-
需要一个计算器来了解需求..:)
标签: javascript jquery jsp checkbox textbox