简体   繁体   English

如何让我的按钮名称作为文本输入的答案?

[英]How do I get my button name to act as my answer for a text input?

I'm currently doing a rock paper scissors game where it should take the button name(rock, paper, or scissors) and use that as the player input.我目前正在做一个石头剪刀布游戏,它应该采用按钮名称(石头、纸或剪刀)并将其用作玩家输入。

So far my project takes the player's choice by prompting a text box where I can type the answer and works as expected.到目前为止,我的项目通过提示一个文本框来接受玩家的选择,我可以在其中输入答案并按预期工作。 Is there a way in javascript where I can take the text from the buttons and convert that into the answer? javascript 中是否有一种方法可以从按钮中获取文本并将其转换为答案?

Or或者

 document.querySelectorAll("button").forEach(function(button) { button.addEventListener('click', function(ev) { var value = button.getAttribute("data-type"); console.log("you pressed " + value); }) })
 <button data-type="rock">Rock</button> <button data-type="paper">Paper</button> <button data-type="scissors">Scissors</button>


<button onclick="submit('rock')">Rock</button>
<button onclick="submit('paper')">paper</button>
<button onclick="submit('scissors')">scissors</button>
function submit(answer){
    // there's your answer: answer (it is a local variable)
}

you can easily use this code to get name of button witch you click.您可以轻松地使用此代码来获取您单击的按钮的名称。 i use p tag to show the name of button witch clicked.我使用 p 标签来显示按钮女巫点击的名称。 you can remove it and use the javascript code to youer next use您可以将其删除并使用 javascript 代码供您下次使用

 function getInfo(event){ document.getElementById('p').innerHTML = event.target.innerHTML }
 <button type="button" onclick="getInfo(event)">Rock</button> <button type="button" onclick="getInfo(event)">paper</button> <button type="button" onclick="getInfo(event)">scissors</button> <p id="p"></p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的JQuery作用于我的按钮而不是输入元素 - How to get my JQuery to act on my button rather than the input element 如何获取分配给带有 textContent 的变量的内容,以作为 javascript 中我的播放器输入的参数? - how can i get what is being assigned to a variable with textContent to act as an argument for my player input in javascript? 我如何让我的 html 输入元素影响我的 javascript 类对象值(字符名称) - How do i get my html input element to influence my javascript class object value(character name) 如何使纯文本充当点击按钮? - How do I make plain text act as a button on click? 我的 React 应用程序中有按钮和教科书,如何使单击按钮触发文本输入? - I have buttons and a textbook in my React App, How do I make the click of the button trigger text input? 如何像 iPhone 一样在 HTML 文本输入框中放置一个清除按钮? - How do I put a clear button inside my HTML text input box like the iPhone does? 如何获取输入单选按钮的值并根据答案执行某些操作? - How to get the value of an input radio button and do something based on answer? 如何让我的函数找到我的输入参数? - How do I get my function to find my input arguments? 如何使文字显示在滑块顶部? - how do I get my text to appear on top of my slider? 如何使用本地存储让我的文本输入字段记住以前输入的数据? - How do I get my text input field to remember previously entered data by using local storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM