【问题标题】:Assign Value if checked checkedbox to input fields which is next to it如果选中复选框,则将值分配给它旁边的输入字段
【发布时间】:2017-01-03 21:25:11
【问题描述】:

如果选中复选框,我需要将值分配给它旁边的文本输入。 例如,如果我检查cb1,则应将value=1 1 分配给txtcb1。我只需要为选中的复选框执行此操作

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    <input type="checkbox" name="cb1" value="1">  <input class="red" id="txtcb1" type="text" name="fname">
    Name One | Amount <input type="text" name="Amount1" class="auto-sum"> <br>
    <input type="checkbox" name="cb2" value="2"> <input class="red" id="txtcb2" type="text" name="fname"> Name Two | Amount <input type="text" name="Amount2" class="auto-sum"> <br>
    <input type="checkbox" name="cb3" value="3"> <input class="red" id="txtcb3" type="text" name="fname"> Name Three | Amount <input type="text" name="Amount3" class="auto-sum"><br>
    <input type="checkbox" name="cb4" value="4"> <input class="red" id="txtcb4" type="text" name="fname"> Name Four| Amount <input type="text" name="Amount4" class="auto-sum"> <br>
    <br>
    TOTAL  <label id="amountTotal">0 <label>
     AED <br>
    <input type="submit" value="Submit" onsubmit="return validateForm(this)">
</form>

http://codepen.io/anon/pen/MJWbNR?editors=1111

【问题讨论】:

  • 请在本网站上发布您的代码
  • 代码链接在底部
  • 在我看来,将事件 onChange 用于复选框和单选框并不是一个好主意。 onClick 事件在我看来是更好的选择。它的工作更可靠。

标签: javascript jquery


【解决方案1】:

您可以使用next() 选择复选框旁边的输入,如下所示:

$(this).next('input[type="text"]').val(id);

下面的工作sn-p:

function validateForm(myButton) {

  return true;
}

$(document).ready(function() {

  //iterate through each textboxes and add keyup
  //handler to trigger sum event
  $(".auto-sum").each(function() {
    $(this).attr('disabled', true);
    $(this).keyup(function() {
      calculateSum();
    });
  });

  $('input[type=checkbox]').on('change', function() {
    var id = $(this).val();
    var active = $(this).prop('checked');
    if(active){
      $(this).next('input[type="text"]').val(id);
    }else{
      $(this).next('input[type="text"]').val(0);
    }
    $('.auto-sum[name=Amount' + id + ']').attr('disabled', !active);
    calculateSum();
  });

});


function calculateSum() {

  var sum = 0;
  //iterate through each textboxes and add the values
  $(".auto-sum").each(function() {
    if ($(this).prop('disabled')) return;
    //add only if the value is number
    if (!isNaN(this.value) && this.value.length != 0) {
      sum += parseFloat(this.value);
    }

  });
  //.toFixed() method will roundoff the final sum to 2 decimal places
  $("#amountTotal").html(sum.toFixed(2));
}
.red {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
  <input type="checkbox" name="cb1" value="1">
  <input class="red" id="txtcb1" type="text" name="fname">Name One | Amount
  <input type="text" name="Amount1" class="auto-sum">
  <br>
  <input type="checkbox" name="cb2" value="2">
  <input class="red" id="txtcb2" type="text" name="fname">Name Two | Amount
  <input type="text" name="Amount2" class="auto-sum">
  <br>
  <input type="checkbox" name="cb3" value="3">
  <input class="red" id="txtcb3" type="text" name="fname">Name Three | Amount
  <input type="text" name="Amount3" class="auto-sum">
  <br>
  <input type="checkbox" name="cb4" value="4">
  <input class="red" id="txtcb4" type="text" name="fname">Name Four| Amount
  <input type="text" name="Amount4" class="auto-sum">
  <br>
  <br>TOTAL
  <label id="amountTotal">0
    <label>
      AED
      <br>
      <input type="submit" value="Submit" onsubmit="return validateForm(this)">

</form>

【讨论】:

    【解决方案2】:

    您可以在change 处理程序中使用方法next

    $(this).next('[type="text"]').val($(this).val());
    

    【讨论】:

      【解决方案3】:

      尝试使用next()

      function validateForm(myButton){
      
      	  return true;
      	}
      
      	$(document).ready(function () {
      
      	     //iterate through each textboxes and add keyup
      	     //handler to trigger sum event
      	     $(".auto-sum").each(function () {
      	         $(this).attr('disabled', true);
      	         $(this).keyup(function () {
      	             calculateSum();
      	         });
      	     });
      	  
      	  $('input[type=checkbox]').on('change', function() {
      	      var id = $(this).val();
      	      var active = this.checked ? this.value : 0;
      	      var test=$(this).next();
      	      $(test).val(active);
      	      $('.auto-sum[name=Amount' + id + ']').attr('disabled', !active);
      	      calculateSum();
      	  });
      
      	 });
      
      	function calculateSum() {
      
      	     var sum = 0;
      	     //iterate through each textboxes and add the values
      	     $(".auto-sum").each(function () {
      	         if ($(this).prop('disabled')) return;
      	         //add only if the value is number
      	         if (!isNaN(this.value) && this.value.length != 0) {
      	             sum += parseFloat(this.value);
      	         }
      
      	     });
      	     //.toFixed() method will roundoff the final sum to 2 decimal places
      	     $("#amountTotal").html(sum.toFixed(2));
      	 }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
      <input type="checkbox" name="cb1" value="1">  <input class="red" id="txtcb1" type="text" name="fname">
      Name One | Amount <input type="text" name="Amount1" class="auto-sum"> <br>
      <input type="checkbox" name="cb2" value="2"> <input class="red" id="txtcb2" type="text" name="fname"> Name Two | Amount <input type="text" name="Amount2" class="auto-sum"> <br>
      <input type="checkbox" name="cb3" value="3"> <input class="red" id="txtcb3" type="text" name="fname"> Name Three | Amount <input type="text" name="Amount3" class="auto-sum"><br>
      <input type="checkbox" name="cb4" value="4"> <input class="red" id="txtcb4" type="text" name="fname"> Name Four| Amount <input type="text" name="Amount4" class="auto-sum"> <br>
       <br>
      TOTAL  <label id="amountTotal">0 <label>
       AED <br>
       <input type="submit" value="Submit" onsubmit="return validateForm(this)">
        
        </form>

      【讨论】:

      • 它为所有值获得相同的值,而它应该根据复选框 value=1 value=2 获得值.....
      • @Learning 立即尝试。
      【解决方案4】:

      在更改事件回调的复选框中,设置 id 值如下:

       $('input[type=checkbox]').on('change', function() {
        $(this).next('[type="text"]').val($(this).val());
      });
      

      Updated Pen

      【讨论】:

      • 哦。然后只需使用next 获取红色文本框并分配值。我已经更新了我的答案。请检查笔。
      猜你喜欢
      • 2013-07-08
      • 2021-08-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多