在 Javascript 中使用 document.writeln 和输入表单
我正在尝试使用 Javascript 输入表单。在表单上,如果下拉选择是某个选择,则特定的表单字段将显示在下面。
这是我当前拥有的代码,但它似乎不起作用,我猜测 document.writeln
不是正确的方法。如果我添加一个警报以查看它是否正在拉动选择,那么它会起作用,因此添加表单的操作会失败。在 document.writeln
之后,警报甚至不再出现?
<script language="javascript">
var HelpTopic = document.getElementById('type');
HelpTopic.onchange=function() {
if(HelpTopic.value == "Analytics") {
alert("Congrats");
document.writeln('\<li\>
\<label for\=\"confirm\"\>Input name*\<\/label\>
\<input type\=\"text\" name\=\"inputs\" id\=\"names\" required \/\>
\<\/li\>');
} else {
alert("Fails");
}
}
</script>
I'm trying to input a form with Javascript. On the form, if the dropdown selection is a certain selection then specific form fields will appear below.
Here is the code I currently have, but it doesn't seem to work, I am guessing document.writeln
is not the proper method. If I add an alert to see if it is pulling the selection it works, so something with adding the form in is failing. After the document.writeln
the alert doesn't even come up anymore?
<script language="javascript">
var HelpTopic = document.getElementById('type');
HelpTopic.onchange=function() {
if(HelpTopic.value == "Analytics") {
alert("Congrats");
document.writeln('\<li\>
\<label for\=\"confirm\"\>Input name*\<\/label\>
\<input type\=\"text\" name\=\"inputs\" id\=\"names\" required \/\>
\<\/li\>');
} else {
alert("Fails");
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 JS 中,不能将一个字符串跨多行。你需要这样的东西:
编辑:你不需要那么多转义。这也同样有效:
You can't span a string over multiple lines in JS. You need something like this:
EDIT: You don't need so much escaping. This will work just as well:
要正确地将列表项添加到某个列表,请使用以下代码:
这会将项目添加到 ID 为
MyList
的列表。To properly add list item to some list have such code instead:
This will add item to list with ID
MyList
.只是一个快速说明,像这样的 JavaScript 支持多行字符串
我同意 Shadow Wizard,这将是附加列表的最佳方式
just a quick note, multi line strings are supported in javascript like this
I agree with Shadow Wizard, that would be the best way to append a list
您尝试动态修改“onChange”(缺少大写C)的方式似乎在Chrome中遇到问题
此外,您不能像您那样将字符串拆分为多行
以下代码应该可以工作,请注意我添加了“onChange =” myfunc();"到选择:
The way you're trying to dynamically modify the "onChange" (missing capital C) seems to have trouble with Chrome
Also, you cant split a string over multiple lines the way you did
The following code should work, notice I added "onChange=myfunc();" to the select: