Referring to the solution of https://stackoverflow.com/a/46159513/1620626 with a big help from @osdavison.
Now I want the script to find it's child class
or id
without predefined it. All I thinking now is making a function
. So I'd hope to do this.
JS
function cloneAllz(chkBoxID, clsName) {
alert(chkBoxID + '/' + clsName);//test value
var $input1 = $(document).find('#' + clsName).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 ($('#' + chkBoxID).is(':checked')) { //if checkbox empty
$('*[id^="' + clsName + '"]').val($this.val()).attr('readonly', true); //clone all inputs and set them readonly
$input1.attr('readonly', false);
}
}, 0);
});
}
HTML
1.
<input type="text" value="" id="box1" />
<label>
<input type="checkbox" id="cloneAll" onChange="cloneAllz('cloneAll','box')" />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 />
All I got from the console is Uncaught ReferenceError: cloneAllz is not defined
Fiddle here
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…