检测选项值的代码未按预期工作
我试图在 javascript 中进行一些字符串比较。我看过一些教程和示例,但它们似乎不起作用。我错过了一些基本的东西吗?
尝试 1
function addAspect(aspect) {
var print = document.createElement('p');
var ptext;
if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}
不起作用。
然后我发现这个例子,所有读者都说它工作正常但
function addAspect() {
var print = document.createElement('p');
var ptext;
var aspect = String(document.getElementById("aspectResult").value);
if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}
不起作用。
我还尝试了 .toString()
以及 '==='
精确匹配比较。
完整代码
<html>
<head>
<script type="text/javascript">
function addAspect()
{
var print = document.createElement('p');
var ptext;
var aspect = document.getElementById("aspectResult").value;
if (aspect == "Option1"){
ptext = document.createTextNode("This is option1");
}
print.appendChild(ptext);
document.getElementById("mainBlock").appendChild(print);
}
</script>
</head>
<body>
<form>
<select id="aspectResult">
<option value="Option1">Option1</option>
</select>
<input type="button" value="Check" onclick="addAspect()"/>
</form>
<span id="mainBlock"> </span>
</body>
</html>
有什么建议吗?
I was attempting to do some string comparisons in javascript. I have seen several tutorials and examples but they do not seem to work. I am missing something fundamental?
Attempt 1
function addAspect(aspect) {
var print = document.createElement('p');
var ptext;
if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}
Doesnt work.
Then I found this example to which all readers said it worked fine
function addAspect() {
var print = document.createElement('p');
var ptext;
var aspect = String(document.getElementById("aspectResult").value);
if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}
Doesnt work.
I also tried .toString()
as well as the '==='
exact match comparison.
Full code
<html>
<head>
<script type="text/javascript">
function addAspect()
{
var print = document.createElement('p');
var ptext;
var aspect = document.getElementById("aspectResult").value;
if (aspect == "Option1"){
ptext = document.createTextNode("This is option1");
}
print.appendChild(ptext);
document.getElementById("mainBlock").appendChild(print);
}
</script>
</head>
<body>
<form>
<select id="aspectResult">
<option value="Option1">Option1</option>
</select>
<input type="button" value="Check" onclick="addAspect()"/>
</form>
<span id="mainBlock"> </span>
</body>
</html>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,简单介绍一下下拉列表的工作原理:
要从下拉列表中读取所选值,您应该执行以下操作:
,创建内部包含文本节点的
元素:
然后 可以将新创建的段落附加到您选择的容器中:
现在就一起来吧!
First, a small introduction to how dropdowns work:
To read the selected value from the dropdown, you should do this:
Then, you create the
<p>
element with a text node inside:Afterwards, you can append the newly created paragraph to the container of your choice:
All together now!
如果您尝试将
ptext
添加到段落中,则需要在末尾添加两行:If you are trying to add the
ptext
to the paragraph, you need to add two lines to the end:您的函数创建了一个文本节点,但随后不对其执行任何操作,因此您的代码似乎根本不执行任何操作。您需要将文本节点附加到 DOM 中某处的元素:
您的完整代码似乎在 IE9、Firefox 4 和 Chrome 11 中工作正常。请参阅 http://jsbin.com/ekura5/。
Your function creates a text node but then does nothing with it, so you code appears to do nothing at all. You need to append the text node to an element somewhere in the DOM:
Your full code appears to be working fine in IE9, Firefox 4 and Chrome 11. See http://jsbin.com/ekura5/.