在 Javascript 中使用 document.writeln 和输入表单

发布于 2024-10-28 03:32:19 字数 712 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人事已非 2024-11-04 03:32:19

在 JS 中,不能将一个字符串跨多行。你需要这样的东西:

document.writeln('\<li\>' +
  '\<label for\=\"confirm\"\>Input name*\<\/label\>' +
  '\<input type\=\"text\" name\=\"inputs\" id\=\"names\" required \/\>' +
'\<\/li\>');

编辑:你不需要那么多转义。这也同样有效:

document.writeln(
  '<li>' +
   '<label for="confirm">Input name*</label> ' +
   '<input type="text" name="inputs" id="names" required />' +
  '</li>');

You can't span a string over multiple lines in JS. You need something like this:

document.writeln('\<li\>' +
  '\<label for\=\"confirm\"\>Input name*\<\/label\>' +
  '\<input type\=\"text\" name\=\"inputs\" id\=\"names\" required \/\>' +
'\<\/li\>');

EDIT: You don't need so much escaping. This will work just as well:

document.writeln(
  '<li>' +
   '<label for="confirm">Input name*</label> ' +
   '<input type="text" name="inputs" id="names" required />' +
  '</li>');
○闲身 2024-11-04 03:32:19

要正确地将列表项添加到某个列表,请使用以下代码:

var oList = document.getElementById("MyList");
var oItem = document.createElement("li");
oItem.innerHTML = "<label for='confirm'>Input name*</label><input type='text' name='inputs' required />";
oList.appendChild(oItem);

这会将项目添加到 ID 为 MyList 的列表。

To properly add list item to some list have such code instead:

var oList = document.getElementById("MyList");
var oItem = document.createElement("li");
oItem.innerHTML = "<label for='confirm'>Input name*</label><input type='text' name='inputs' required />";
oList.appendChild(oItem);

This will add item to list with ID MyList.

悲欢浪云 2024-11-04 03:32:19

只是一个快速说明,像这样的 JavaScript 支持多行字符串

'<li>\
  <label for="confirm">Input name*</label>\
  <input type="text" name="inputs" id="names" required />\
</li>'

我同意 Shadow Wizard,这将是附加列表的最佳方式

just a quick note, multi line strings are supported in javascript like this

'<li>\
  <label for="confirm">Input name*</label>\
  <input type="text" name="inputs" id="names" required />\
</li>'

I agree with Shadow Wizard, that would be the best way to append a list

偷得浮生 2024-11-04 03:32:19

您尝试动态修改“onChange”(缺少大写C)的方式似乎在Chrome中遇到问题

此外,您不能像您那样将字符串拆分为多行

以下代码应该可以工作,请注意我添加了“onChange =” myfunc();"到选择:

<select id=type onChange=myfunc();>";
<option value=Other>Other</option>
<option value=Analytics>Analytics</option>
</select>


<script language="javascript">
function myfunc()
{
var HelpTopic = document.getElementById('type');

if(HelpTopic.value == "Analytics") {
    alert("Congrats");
    document.write('<li><label for="confirm">Input name*</label><input type="text" name="inputs" id="names" required></li>');
} else {
    alert("Fails");
}

}
</script>

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:

<select id=type onChange=myfunc();>";
<option value=Other>Other</option>
<option value=Analytics>Analytics</option>
</select>


<script language="javascript">
function myfunc()
{
var HelpTopic = document.getElementById('type');

if(HelpTopic.value == "Analytics") {
    alert("Congrats");
    document.write('<li><label for="confirm">Input name*</label><input type="text" name="inputs" id="names" required></li>');
} else {
    alert("Fails");
}

}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文