【问题标题】:dynamically populate div from js array in html form以html形式从js数组动态填充div
【发布时间】:2012-04-16 22:33:00
【问题描述】:

我基本上是 javascript 的新手。当用户选择他想要查看的书和章节并按下提交按钮时,我想从同一页面上的选择表单中填充我的网页中的 div。代码如下:

<script type="text/javascript" charset="utf-8">
          var setArray = newArray(
                 bk1, 001, "this is my chapter content for the <div>",
                 bk1, 002, "this is my chapter content for the <div>",
                 bk1, 003, "this is my chapter content for the <div>",
                 bk2, 001, "this is my chapter content for the <div>",
                 bk2, 002, "this is my chapter content for the <div>"
                 etc....
          );
</script>

<form>
<select id="book">
  <option value="">Select Book</option>
  <option value="bk1">Book 1</option>
  <option value="bk2">Book 2</option>
  <option value="bk3">Book 3</option>
</select>
<select id="chapter">
  <option value="">Select Chapter</option>
  <option value="001">1</option>
  <option value="002">2</option>
  <option value="003">3</option>
</select>
<button id="button" type="submit">View</button>
</form>

<div id="content">
I want the content from the js array for the selected book and chapter to be placed here without reloading the page.
</div>

注意:我已简化代码以使其更易于理解。我确定我的 js 数组不正确,需要为我的目的进行修复。另外,我不希望页面刷新,只更新 div。感谢您的帮助。

【问题讨论】:

  • 试试ajax!!!!!!!!!!!!!!!!!!!!!1
  • 理想情况下,您会想要一个更好的结构数组。例如,一个对象数组,其中每个对象都有一个 book、champ 和 comment 属性。然后你可以搜索一个事件的数组。当然,无论如何,整个“硬编码”选项都是一个坏主意。您可以使用 AJAX 查询服务器并查找数据库(例如)
  • 您应该考虑使用 Ajax 库。我建议你使用 jQuery,它还有很多其他方便的功能。

标签: javascript arrays forms html


【解决方案1】:

如果这是一个大型网站,您需要学习如何使用 jQuery 和 Ajax(不刷新所有元素)。几本书?和章节?

【讨论】:

  • 这是一个答案吗...?请发表评论。
【解决方案2】:

如果你可以像这样将你的数组格式化成多维数组

   var setArray = ["book1":["chap1":"content","chap2":"content"], "book2":["chap1":"content","chap2":"content"]]

那么很容易解析数组,如果你调用setArray["book1"]["chap1"]就会得到那个内容

【讨论】:

  • 好的,谢谢。如何从表单中进行选择并从数组的“内容”中填充 div?
  • use onchange event listener for select box, when the select box change it will call the function and there you can capture the value of select box, using that select box value you can get content
【解决方案3】:

正如建议的那样,您应该认真考虑使用 ajax 来检索这些数据,它不会使页面刷新,并且允许您将这些数据保持为半私密,并且在后端更易于管理。

如果/当您使用 ajax 执行此操作时,您仍然需要以下内容:

var booksData = {
    book1: [
        "chapter 1 content",
        "chapter 2 content",
        "..."
    ],
    book2: [
        "chapter 1 content",
        "chapter 2 content",
        "..."
    ]
}

function whenButtonClicked() {
    var book = "book1" // get the actual book name from the select input
    var chapter = 0 // get the selectedIndex of chapter input
    var content = booksData[book][chapter];
    var div = document.getElementById("content");
    div.innerHTML = content;
}

并在按钮上使用 onclick 处理程序。类似的东西

<button id="button" onclick="whenButtonClicked()">

我建议查看像 jQuery 这样的库,它可以让生活更轻松,并清理很多内容。

【讨论】:

  • 非常感谢……它真的很有帮助。在您的数组中,我对章节编号的去向感到有些困惑。似乎“第 1 章内容”只是内容......从表单中调用的数组中的 001、002、003 等在哪里?再次感谢。
  • 哦,我明白了...您依赖数组的索引,并且将使用选择的索引(而不是值),对吗?
  • 其实我是用jquery来管理近70本书和近2000章的。我使用它来链接两个选择,以便章节选择仅显示已选择书籍的选项。顺便说一句,我认为这会干扰按钮。当我对按钮执行 onclick 命令时,内容会短暂显示在 div 中,然后消失。不知道为什么,也许 jquery 插件搞砸了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 2018-08-18
  • 2018-05-28
相关资源
最近更新 更多