【发布时间】:2015-09-02 08:24:18
【问题描述】:
以下代码应显示所选 ID 的结果:
<form id="<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">
Click <a href="#" onclick="document.getElementById('<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>
但是当我点击链接时,它什么也不做(不引导我到 show.php)。
但是,如果我将 onclick="document.getElementById('<?= $blog_id ?>') 更改为 onclick="document.getElementById('test') (以及另一个 ID)。
它引导我到 show.php,但显示了最高 ID。
为什么我需要 id 作为变量?因为它在一个循环中,当我点击链接时,我想从选定的 id 中获取正确的信息。
我查看了我的控制台(检查元素),它说:
Uncaught TypeError: document.getElementById(...).submit is not a function
但是我不知道如何解决这个问题。
【问题讨论】:
-
不使用内联JS,可以动态绑定事件处理器吗?例如。使用
obj.addEventListener()。 -
基于javascript,我非常缺乏经验,能否请您详细介绍一下obj.addEventListener()
-
尝试使用 "" 而不是 '' 点击 这里 查看 (= count($blog_cmets) ?>) cmets。
标签: javascript php html css foreach