简体   繁体   English

使用占位符(用于输入框)的值作为JS中的条件

[英]Using the value of a placeholder (for an input box) as a condition in JS

(This is all made with HTML and JS) I made this program that takes a decimal number, converts it into binary and gives you the result. (这全部是用HTML和JS制作的)我制作了这个程序,该程序使用一个十进制数字,将其转换为二进制并提供结果。 The input box that takes the decimal number has a placeholder 'Enter decimal number'. 带有十进制数字的输入框具有占位符“输入十进制数字”。 After I made the decimal to binary part functional, I started on making a reverse button that would first change the placeholder to 'Enter binary number'. 在使小数到二进制部分起作用后,我开始制作一个反向按钮,该按钮首先将占位符更改为“输入二进制数”。 These are the HTML and JS codes: 这些是HTML和JS代码:

 function reverseButton() { if (document.getElementsByClassName('inputBox').placeholder === 'Enter decimal number') { document.getElementsByClassName('inputBox').placeholder='Enter binary number'; } else if (document.getElementsByClassName('inputBox').placeholder === 'Enter binary number') { document.getElementsByClassName('inputBox').placeholder='Enter decimal number'; } } 
 <input type="number" class="inputBox" id="decimalNumber" placeholder="Enter decimal number" min="0" step="0.0001"> <button class="button hoverTurquoise reverse" onclick="reverseButton()">Reverse</button> 

I tried to first set the place holder to nothing (empty '') and then adding the text but I get the same result. 我尝试先将占位符设置为无(空”),然后添加文本,但得到的结果相同。 If anyone knows what to do, your help would be greatly appreciated. 如果有人知道该怎么办,将非常感谢您的帮助。

getElementsByClassName函数返回一个数组,因此您必须将代码转换为这种形式

if (document.getElementsByClassName('inputBox')[0].placeholder === 'Enter decimal number') {

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM