【发布时间】: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 />
问题是:
- 如何找到每组class/id的第一个输入并将其设置为原型?
- 选中复选框时如何继续输入以下输入而不卡住?
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