【发布时间】: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