javascript – 基于复选框选中/取消选中的文本框值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 基于复选框选中/取消选中的文本框值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3638字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 基于复选框选中/取消选中的文本框值](/upload/InfoBanner/zyjiaocheng/776/2a394a2515a1409191371bc44dd3e561.jpg)
我的问题很简单,不像看起来那么冗长.
我有7个文本框,其中包含ids text1,text2,text3,text4和text5,text6,text7和一个具有id检查的复选框.我通过JSON获得text1和text2的值为10和20,并在text7中替换总计30. text4,text5和text6的值为空,这些文本框显示为“0”. text3和text4将根据复选框自动填充.
当我选中复选框时,“Points”将在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:http://jsfiddle.net/Af6LP/1/根据您的需要进行修改.
脚本
$(document).ready(function() {
$("#check").change(function() {
if ($(this).is(":checked")) $("#text4").val("100");
else $("#text4").val("0");
calculate();
});
$("#text1, #text2, #text5, #text6").keyup(function() {
calculate();
});
});
function calculate() {
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);
if ($("#check").is(":checked")) total += parseInt($("#text4").val());
$("#text7").val(total);
}?
HTML
<body>
<input type="checkbox" id="check"/><br/>
<input type="text" id="text1" value="10"/><br/>
<input type="text" id="text2" value="20"/><br/>
// how can i autofill "POINTS" in this textbox after checking the checkbox?
<input type="text" id="text3"/><br/>
//how can i autofill "100" in this textbox after checking the checkbox?
<input type="text" id="text4" value="0"/><br/>
<input type="text" id="text5" value="0"/><br/>
<input type="text" id="text6" value="0"/><br/>
// shows total of all values of above textboxes except text3<br/>
<input type="text" id="text7" value="30"/>
</body>?
内容总结
以上是互联网集市为您收集整理的javascript – 基于复选框选中/取消选中的文本框值全部内容,希望文章能够帮你解决javascript – 基于复选框选中/取消选中的文本框值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。