【问题标题】:How to clone typing to every input from the first box with jquery on checkbox checked?如何在选中复选框的 jquery 的情况下从第一个框中克隆输入到每个输入?
【发布时间】:2018-02-19 21:47:43
【问题描述】:

我想将每列的第一个输入的输入克隆/复制到同一类/id 的子框中。

例如,有 5 列数据。每列都有自己的类和特定的 ID。一旦我开始在每列的顶部输入中输入并选中复选框。该列的以下/子输入开始输入相同的笔划。

JS

var $input1 = $(document).find('input[id^="box"]').filter(':visible:first'); //find the first input begins with *box or other same id???
$($input1).keypress(function() { //duplicate the first box typing
  var $this = $(this);
  window.setTimeout(function() { //delay a bit
    if ($('#cloneAll').is(':checked')) { //if checkbox empty
      $('*[id^="box"]').val($this.val()).attr('readonly', true); //clone all inputs and set them readonly
    }
  }, 0);
});

html

 1.<input type="text" value="" id="box1" /><label><input type="checkbox" id="cloneAll" />clone all</label>
<br /> 2.<input type="text" value="" id="box2" />
<br /> 3.<input type="text" value="" id="box3" />
<br /> 4.<input type="text" value="" id="box4" />
<br /> 5.<input type="text" value="" id="box5" />
<br /> .
<br /> .
<br /> .
<br /> 100.<input type="text" value="" id="box100" /><br />

问题是:

  1. 如何找到每组class/id的第一个输入并将其设置为原型?
  2. 选中复选框时如何继续输入以下输入而不卡住?

Fiddle

var $input1 = $(document).find('input[id^="box"]').filter(':visible:first'); //find the first input begins with *box or other same id???
$($input1).keypress(function() { //duplicate the first box typing
  var $this = $(this);
  window.setTimeout(function() { //delay a bit
    if ($('#cloneAll').is(':checked')) { //if checkbox empty
      $('*[id^="box"]').val($this.val()).attr('readonly', true); //clone all inputs and set them readonly
    }
  }, 0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1.
<input type="text" value="" id="box1" />
<label>
  <input type="checkbox" id="cloneAll" />clone all</label>
<br /> 2.
<input type="text" value="" id="box2" />
<br /> 3.
<input type="text" value="" id="box3" />
<br /> 4.
<input type="text" value="" id="box4" />
<br /> 5.
<input type="text" value="" id="box5" />
<br /> .
<br /> .
<br /> .
<br /> 100.
<input type="text" value="" id="box100" />
<br />

【问题讨论】:

  • 您将所有框设置为只读。这就是你不能一直打字的原因
  • 因为我希望它遵循 first 输入的输入(如果它的 ID)。那么如何排除呢?
  • 您已经将第一个框作为变量,因此只需在将所有框设置为只读的行之后添加$input1.attr('readonly', false);
  • 好的,它正在工作! => jsfiddle.net/be9br09j/2
  • 另外,您不需要在第二行的 $input1 周围加上括号。可以是$input1.keypress(function() {

标签: javascript jquery html checkbox


【解决方案1】:

您代码中的这一行将所有输入框设置为只读,并阻止您在第一个框中输入。

$('*[id^="box"]').val($this.val()).attr('readonly', true); //clone all inputs and set them readonly

如果您在其下方添加此行,它将允许您继续在第一个框中输入。

$input1.attr('readonly', false);

更新小提琴: http://jsfiddle.net/be9br09j/2/

更新片段

var $input1 = $(document).find('input[id^="box"]').filter(':visible:first'); //find the first input begins with *box or other same id???
$($input1).keypress(function() { //duplicate the first box typing
  var $this = $(this);
  window.setTimeout(function() { //delay a bit
    if ($('#cloneAll').is(':checked')) { //if checkbox empty
      $('*[id^="box"]').val($this.val()).attr('readonly', true); //clone all inputs and set them readonly
      $input1.attr('readonly', false);
    }
  }, 0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1.
<input type="text" value="" id="box1" />
<label>
  <input type="checkbox" id="cloneAll" />clone all</label>
<br /> 2.
<input type="text" value="" id="box2" />
<br /> 3.
<input type="text" value="" id="box3" />
<br /> 4.
<input type="text" value="" id="box4" />
<br /> 5.
<input type="text" value="" id="box5" />
<br /> .
<br /> .
<br /> .
<br /> 100.
<input type="text" value="" id="box100" />
<br />

【讨论】:

  • 我可以再问一点吗?如果有超过 1 组这些输入,并且我不想在每次想要克隆它时更改代码怎么办。告诉程序遵循输入的最佳方法是什么?
  • 通常不鼓励在 cmets 中提问。请询问new question 并在此处发布链接,我会尽力提供帮助。
  • 我在这里有一个新线程:stackoverflow.com/q/46159961/1620626。请帮帮我(-/\-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2016-09-22
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多