文本输入作为数组的变量,在javascript上有一个while循环

时间:2014-10-03 01:16:34

标签: javascript loops

我正在试图弄清楚如何使文本框号反映我想要显示的数组上的数字。附上我的代码。我想说的是,如果我键入" 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>

1 个答案:

答案 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查看一个有效的示例。

希望它有用!