单击按钮(John)时,我试图在文本框中输出插入的值。目前我刚刚在这里添加了默认值(John= new Player(30,"England",3)); 但我希望从文本框中选择这些值。请告知我将如何去做。我尝试从文本框中获取值并分配给变量(x,y,z在脚本中注释掉)并在此处传递(John = new Player(x,y,z));但它似乎没有用。
Age : <input type = "text" id = "test1" /><br>
County : <input type = "text" id = "test2" /><br>
Rank: <input type = "text" id = "test3" /><br>
<button type="button" onclick="John.myPlayer()">John</button>
<br>
<br>
<div id = "box"></div>
<br>
<script type="text/javascript">
// var x = document.getElementById('test1').value;
// var y = document.getElementById('test2').value;
// var z = document.getElementById('test3').value);
function Player(a,c,r){
this.age=a;
this.country=c;
this.rank=r;
}
Player.prototype.myPlayer = function(){
document.getElementById('box').innerHTML = "John is from " + this.country + " he is " + this.age + " years old and his rank is " + this.rank;
}
John= new Player(30,"England",3);
John.myPlayer()
</script>