【问题标题】:php in a javascript function not workingjavascript函数中的php不起作用
【发布时间】: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('&lt;?= $blog_id ?&gt;') 更改为 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


【解决方案1】:

我写了一个简单的测试,它成功了

也许 ryrysz 是对的。

test.php

<?php for($i=1; $i<11; $i++): ?>
<?php  
    $blog_id =$i;
    $blog_title = 'abc'.$i;
    $blog_date = '11-02-2012';
    $blog_content = 'dsfdsfdfsdfsdfs'.$i;
    $blog_comments[] = '';
?>
<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>
<?php endfor ?>

显示.php

<?php
print_R($_POST);

【讨论】:

    【解决方案2】:

    IMO 将表单的 ID 设置为数字并不是一个好主意:

    第一标准:

    ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,并且可以是 后跟任意数量的字母、数字 ([0-9])、连字符 ("-")、 下划线(“_”)、冒号(“:”)和句点(“.”)。

    但是,在您的情况下,可能是代码中重复 ID 的问题。也许您有多个具有此 ID 的元素?

    试试这个:

        <form id="form_<?= $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('form_<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
        </form>
    

    【讨论】:

    • 这些是 HTML4 的标准。 HTML5 现在更加宽松,并且允许您设置仅限数字的 id,尽管我同意这不是一个好主意。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多