我正在试图弄清楚如何使文本框号反映我想要显示的数组上的数字。附上我的代码。我想说的是,如果我键入" 4"然后我的4个技能会弹出我的网站。
<script type="text/javascript">
function myduties1() {
var a= document.getElementById("DNumber1").value;
var duties1= ["Building Houses","Using Heavy Machinery","Lifting","Fixing","Working Fast"];
var b= a-1
var c=""
while (a>=b) {
c== "duties1[b]";
b++;
}
document.getElementById("mduties1").innerHTML =c;
}
</script>
<p><button onclick="myduties1()">Click Here</button>to see my top <input type="text" id="DNumber1" value=""> job duties</p>
<p id="mduties1"></p>
答案 0 :(得分:0)
可能的解决方案:
function myduties1() {
var a= document.getElementById("DNumber1").value;
var duties1= ["Building Houses","Using Heavy Machinery","Lifting","Fixing","Working Fast"];
var out = "";
if (a <= duties1.length){
var out = (duties1.slice(0,a)).join();
}
document.getElementById("mduties1").innerHTML =out;
}
点击此链接jsfiddle查看一个有效的示例。
希望它有用!