【发布时间】:2017-09-12 12:18:48
【问题描述】:
参考https://stackoverflow.com/a/46159513/1620626的解决方案,得到@osdavison的大力帮助。
现在我希望脚本在没有预定义的情况下找到它的子 class 或 id。我现在只想做一个function。所以我希望这样做。
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 />
我从控制台得到的只是Uncaught ReferenceError: cloneAllz is not defined
【问题讨论】:
标签: javascript jquery html