-1

所以这是我的代码。我想创建像这样的按钮

随机 1 随机 2 .... ... 随机 10

有什么方法可以通过 JavaScript 中的 for 循环来实现吗?id 也是如此,就像第一个按钮的 id 是 "button1" ....

<script type="text/javascript">
<!--
  var i = 0;
  var x = new Array(11);
  var y = new Array(11);
  for (i=1; i<=10; i++)
  {
  x[i] = 35.38825899+Math.random()*0.007962;
  y[i] = 0.03903201/0.007962*x;
  document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');
  }
//-->
</script>
4

4 回答 4

0

简单的。更改:

value="Random"

至:

value="Random ' + i + '"

最终代码

document.write('<input type="button" id="button'+i+'" value="Random'+i+'" onclick="sfcshonan()"/>'+'<br />');
于 2013-05-16T06:41:36.557 回答
0
document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');

document.write('<input type="button" id="button' + i + 'value="Random' + i + '" onclick="sfcshonan()"/>'+'<br />');


或者使用 jQuery

var button = $("<input>").attr("type", "button").attr("id", "button" + i).val("Random" + i).click(sfcshonan)

$(document).append(button)
于 2013-05-16T06:36:34.867 回答
0

尝试这个

   document.write('<input type="button" id="button'+i+'" value="Random'+i+'" onclick="sfcshonan()"/>'+'<br />');

安装在

   document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');
于 2013-05-16T06:37:34.777 回答
0
document.write('<input type="button" value="Random '+i+'" onclick="sfcshonan()"/>'+'<br />');
于 2013-05-16T06:41:06.290 回答