Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

javascript - How to clone typing to every input from the first box with jquery on checkbox checked (dynamically)?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly your fiddle is using jQuery 1.5.2 which is very outdated. You should update that ASAP.

Secondly you should use unobtrusive event handlers instead of event attributes in the HTML.

To fix your actual problem you can hook to the input event of the first input. Within that event handler you can inspect to see if the checkbox is checked, and if so se the val() of all other inputs to match that of the current one. Using this method will massively simplify your code which currently seems a little over-complicated. Try this:

$('.container .master').on('input', function() {
  var $container = $(this).closest('.container');
  if ($container.find('.cloneAll').is(':checked'))
    $container.find('input:not(.master)').val(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  1. <input type="text" value="" class="master" />
  <label>
    <input type="checkbox" class="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" />
</div><br /><br />

<div class="container">
  1. <input type="text" value="" class="master" />
  <label>
    <input type="checkbox" class="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" />
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...