简体   繁体   English

接收[object HTMLInputElement]而不是我的Variable

[英]Receiving [object HTMLInputElement] instead of my Variable

I have a .js file in the same location as the HTML file and what I am trying to do is when I click on a Radio Button, information displays. 我有一个与HTML文件位于同一位置的.js文件,而我要执行的操作是单击单选按钮时,显示信息。 I got that working on 4 out of 5 of the buttons. 我在5个按钮中的4个上工作了。 The last one is just displaying the error instead of the Var from the .js file. 最后一个只是显示错误,而不是.js文件中的Var。

<p> <!-- Radio buttons for drinks -->
    <strong>Select your drink:</strong>
    <br /><br />
    <input type="radio" name="drink" onclick="document.drinkList.drinkDesc.value = black" /> Black Coffee
    <br />
    <input type="radio" name="drink" onclick="document.drinkList.drinkDesc.value = loco" /> Loco Mocha
    <br />
    <input type="radio" name="drink" onclick="document.drinkList.drinkDesc.value = vanilla" /> Vanilla Bean Frappuccino
    <br />
    <input type="radio" name="drink" onclick="document.drinkList.drinkDesc.value = pumpkin" /> Pumpkin Spice Latte
    <br />
    <input type="radio" name="drink" onclick="document.drinkList.drinkDesc.value = caramel" /> Caramel Macchiato
    <br />
</p>
<p><!-- Design for output -->
    <textarea name="drinkDesc" cols="120" rows="1" style="background-color: Transparent; border: none; resize: none; overflow: hidden; font: 16px arial; color: #3F3F3F"></textarea>
</p>

And the .js file is just. .js文件就是。

var black = "Plain black coffee brewed from our finest of roasted coffee beans."
var loco = "Chocolate mocha flavored, covered in chocolate chips and topped with whipped cream."
var vanilla = "Vanilla flavored iced drink, topped with whipped cream and caramel. (No coffee added)"
var pumpkin = "Pumpkin flavored latte, with espresso and milk mixed together for a delicious beverage."
var caramel = "Steamed milk with vanilla-flavored syrup is marked with espresso and topped with caramel drizzle."

Thank you! 谢谢!

I've made 1 change: 我进行了1次更改:

used getElementById instead of document.[elementname] 使用了getElementById而不是document。[elementname]

  var black = "Plain black coffee brewed from our finest of roasted coffee beans." var loco = "Chocolate mocha flavored, covered in chocolate chips and topped with whipped cream." var vanilla = "Vanilla flavored iced drink, topped with whipped cream and caramel. (No coffee added)" var pumpkin = "Pumpkin flavored latte, with espresso and milk mixed together for a delicious beverage." var caramel = "Steamed milk with vanilla-flavored syrup is marked with espresso and topped with caramel drizzle." 
 <p> <!-- Radio buttons for drinks --> <strong>Select your drink:</strong> <br /> <br /> <input type="radio" name="drink" onclick="document.getElementById('drinkDesc').value = black" /> Black Coffee <br /> <input type="radio" name="drink" onclick="document.getElementById('drinkDesc').value = loco" /> Loco Mocha <br /> <input type="radio" name="drink" onclick="document.getElementById('drinkDesc').value = vanilla" /> Vanilla Bean Frappuccino <br /> <input type="radio" name="drink" onclick="document.getElementById('drinkDesc').value = pumpkin" /> Pumpkin Spice Latte <br /> <input type="radio" name="drink" onclick="document.getElementById('drinkDesc').value = caramel" /> Caramel Macchiato <br /> </p> <p> <!-- Design for output --> <textarea id='drinkDesc' cols="120" rows="1" style="background-color: Transparent; border: none; resize: none; overflow: hidden; font: 16px arial; color: #3F3F3F"></textarea> </p> 

The reason why you are getting [object HTMLInputElement] is because the value of caramel is referring to an element via being a global variable. 之所以要获取[object HTMLInputElement]是因为caramel的值通过成为全局变量来引用元素。 Elements in the HTML with an id or a name could be a global variable to the window in JavaScript. HTML中具有idname元素可以是JavaScript中窗口的全局变量。 So if an element has id="caramel" or name="caramel" it will reffer to that element as caramel in JS. 因此,如果元素具有id="caramel"name="caramel" ,它将在JS中引用为caramel A simple solution is to use a different name for either the var caramel (such as Caramel ) or change the value of the id/name in the HTML element. 一个简单的解决方案是对var caramel (例如Caramel )使用不同的名称,或者更改HTML元素中id/name的值。

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

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