基于复选框的文本框值已选中/未选中

har*_*rry 5 javascript checkbox jquery jsp textbox

我的问题很简单,没有看起来那么冗长。

我有7个具有ID文本1,文本2,文本3,文本4和文本5,文本6,文本7的文本框以及一个具有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>
Run Code Online (Sandbox Code Playgroud)

Ama*_*ure 3

你可以查看这个jsFiddle: http: //jsfiddle.net/Af6LP/1/根据你的需要修改它。

\n\n

脚本

\n\n
$(document).ready(function() {\n  $("#check").change(function() {\n      if ($(this).is(":checked")) $("#text4").val("100");\n      else $("#text4").val("0");\n      calculate();\n  });\n\n  $("#text1, #text2, #text5, #text6").keyup(function() {\n      calculate();\n  });\n});\n\nfunction calculate() {\n  var a = $("#text1").val();\n  var b = $("#text2").val();\n  var c = $("#text5").val();\n  var d = $("#text6").val();\n  var total = parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d);\n  if ($("#check").is(":checked")) total += parseInt($("#text4").val());\n  $("#text7").val(total);\n}\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

超文本标记语言

\n\n
<body>\n    <input type="checkbox" id="check"/><br/>\n    <input type="text" id="text1" value="10"/><br/>\n    <input type="text" id="text2" value="20"/><br/>\n    // how can i autofill "POINTS" in this textbox after checking the checkbox?\n    <input type="text" id="text3"/><br/>\n    //how can i autofill "100" in this textbox after checking the checkbox?\n    <input type="text" id="text4" value="0"/><br/>\n    <input type="text" id="text5" value="0"/><br/>\n    <input type="text" id="text6" value="0"/><br/>\n    // shows total of all values of above textboxes except text3<br/>\n    <input type="text" id="text7" value="30"/>\n</body>\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n