admin管理员组文章数量:1414614
I am just learning Javascript and I am trying to create a script that will ask the user to input a number and the script will then print out a result from an array. I have figured out how to do that with the prompt mand but want to learn how to do it using a input field on the webpage so that the user will type in the number, hit a button, and it will then run the script.
Here is what I have written:
Header script:
do {
var num = prompt('Enter a number between 1 and 10?','');
num = parseInt(num, 10);
} while (isNaN(num));
var surge = [
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten'
];
Body script:
document.write(surge[num-1]);
I have no idea how to transform this to an input field/button format and keep the do/while to ensure that a number is entered. Also, how would I change the do/while so that it will also ensure that a number greater than 10 cannot be entered and ensure that it is a number? Any and all help will be greatly appreciated. Thanks!
I am just learning Javascript and I am trying to create a script that will ask the user to input a number and the script will then print out a result from an array. I have figured out how to do that with the prompt mand but want to learn how to do it using a input field on the webpage so that the user will type in the number, hit a button, and it will then run the script.
Here is what I have written:
Header script:
do {
var num = prompt('Enter a number between 1 and 10?','');
num = parseInt(num, 10);
} while (isNaN(num));
var surge = [
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten'
];
Body script:
document.write(surge[num-1]);
I have no idea how to transform this to an input field/button format and keep the do/while to ensure that a number is entered. Also, how would I change the do/while so that it will also ensure that a number greater than 10 cannot be entered and ensure that it is a number? Any and all help will be greatly appreciated. Thanks!
Share Improve this question edited Aug 18, 2014 at 18:56 John Powell 12.6k6 gold badges63 silver badges69 bronze badges asked Jan 12, 2012 at 8:03 Mock26Mock26 711 gold badge3 silver badges9 bronze badges 1- Learn about the DOM. – kapa Commented Jan 12, 2012 at 8:07
1 Answer
Reset to default 4Sample HTML:
<input type='text' id='number' />
<input type='button' id='add' value="click" />
<div id='numbers'> </div>
and script for this
var surge = [ 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten' ];
//helper function for get elements
var get = function( id ) {
return document.getElementById( id );
};
//add handle to click event on '#add' button
get( "add" ).onclick = function () {
// get value of #number input
var n = get( "number" ),
s = surge[ n.value -1];
//if element in surge at index n.value - 1 is present
if( s ) {
// write this in next paragraph which is pushed to div#numbers
get( "numbers" ).innerHTML += "<p>" + s + "</p>";
};
//reset value of input #number
n.value = ""
};
Demo on: http://jsfiddle/a9zZL/1/
本文标签: javascriptInput Number between 110Click ButtonGet ResultStack Overflow
版权声明:本文标题:javascript - Input Number between 1-10, Click Button, Get Result - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745159866a2645391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论