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
233 views
in Technique[技术] by (71.8m points)

insert - Javascript random number in function onclick

Need some help. with the below javascript. On inserting a row I need the random number t to generate a new number on each click (insert). I have to send each row to the processor as an array so that needs to be a unique number on each insert. I can get it to work the first time but of course it's just generating on load and not each click. Been at it for hours trying to figure it out... but no dice.

Any help?

<script type="text/javascript">
     $(document).ready(function(){
     var i=1;
     var t = Math.floor(Math.random() * 256);

      $("#add_row").click(function(){
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='need["+t+"][type]' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='need["+t+"][scope]' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='need["+t+"][priority]' type='text' placeholder='Mobile'  class='form-control input-md'></td>");

      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++;
  });
     $("#delete_row").click(function(){
     if(i>1){
         $("#addr"+(i-1)).html('');
         i--;
         }
     });

  });

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You are correct that you are only generating the random number on page load, because you define t right when the page loads. What you need to do is generate the number inside the click function, so that way

var t = Math.floor(Math.random() * 256);

is called every time you click instead of just once when the document is ready.

So instead of

 $("#add_row").click(function(){
  $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='need["+t+"][type]' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='need["+t+"][scope]' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='need["+t+"][priority]' type='text' placeholder='Mobile'  class='form-control input-md'></td>");

  $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
  i++;

});

you would have:

 $("#add_row").click(function(){
  var t = Math.floor(Math.random() * 256);
  $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='need["+t+"][type]' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='need["+t+"][scope]' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='need["+t+"][priority]' type='text' placeholder='Mobile'  class='form-control input-md'></td>");

  $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
  i++;

});


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

...