0

现在,这就是我的页面的样子:

.
.
.
<?php
for($i=1;$i<3;$i++)
   {
       <a href="#edit" class="dialog_box" onclick="edit_comment('<?php echo $array[$i]['id'];?>','<?php echo $array[$i]['comment'];?>')">Edit</a>
   }
?>// so, there are 3 links now, all are same
// here, $array[$i]['id'] is 1,2,3,....... and $array[$i]['comment'] is "comment 1","comment 2","comment 3", ...... etc.

<div id="edit">
   <h1>A Header</h1>
   <form method="post" id="edit_form" name="edit_form" action="<?php echo site_url("controller/function");?>">
       <input type="hidden" name="edit_id" id="edit_id" />
       <label>Some Label:<input id="edit_comment" name="edit_comment" type="text" size="10" autofocus /></label>
       <input type="submit" value="Ok" />
   </form>
</div>
<div>
.
.
.
</div>
<script>
// some "dialog_box" related work
</script>
<script>
function edit_comment(id,comment){
    $("#edit_id").attr("value",id);
    $("#edit_comment").attr("value",comment);
};
</script>
</body>
</html>

在这里,class="dialog_box" 用于我在这里使用的对话框。

所以,我认为现在更容易寻找错误。请帮帮我。这让我疯狂。

4

4 回答 4

2

看起来问题出在您的 onclick 上。您想将字符串传递给函数,对吗?

<a href="#edit" onclick="edit_comment(<?php echo "id";?>,<?php echo "comment";?>)">Edit</a>

应该

<a href="#edit" onclick="edit_comment('<?php echo \"id\";?>','<?php echo \"comment\";?>')">Edit</a>

这对我有用。有几点需要注意:id 应该以字母开头,而不是数字。您的函数与您的输入同名。我看到以下错误:TypeError: '[object HTMLInputElement]' is not a function (evalating 'edit_comment('1','comment 1')')。控制台是您的朋友。

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script>
   function fn_edit_comment(id,comment){
      console.log(id);
      console.log(comment);
      $("#edit_id").attr("value",id);
      $("#edit_comment").attr("value",comment);
   };
</script>
</head>
<body>

<a href="#edit" onclick="fn_edit_comment('id1','comment 1');">Edit</a>
<a href="#edit" onclick="fn_edit_comment('id2','comment 2');">Edit</a>  
<a href="#edit" onclick="fn_edit_comment('id3','comment 3');">Edit</a> 
<div id="edit">
    <h1>A Header</h1>

    <form method="post" id="edit_form" name="edit_form" action="">
        <input type="hidden" name="edit_id" id="edit_id" />
        <label>Some Label:
            <input id="edit_comment" name="edit_comment" type="text" size="10" autofocus />
        </label>
        <input type="submit" value="Ok" />
    </form>
</div>

</body>
</html>
于 2013-07-16T17:11:19.040 回答
1

我猜你在那里回显字符串:

<a href="#edit" onclick="edit_comment('<?php echo "id";?>','<?php echo "comment";?>')">Edit</a>

注意回声周围的引号!

并确保该edit_comment函数在范围内,换句话说,不在文档就绪函数或类似函数内,然后执行:

function edit_comment(id,comment){
    document.getElementById('#id').value = id;
    document.getElementById('#new_comment').value = comment;
}

请记住,ID 是唯一的,在 ID 中使用带有 ID 的选择器是没有意义的,因为不能有多个相同的 ID。

于 2013-07-16T17:16:23.893 回答
1
function edit_comment(id,comment){
    $("#edit_form #id").attr("value",id);
    $("#edit_form #new_comment").attr("value",comment);
};

#edit从选择器中删除了,因为#edit_form不在里面#edit。并且label是不必要的。

于 2013-07-16T17:19:22.793 回答
0

您的 HTML 表单输入字段 id="id" 没有值,因此您不能指望得到一个值。

<input type="hidden" name="id" id="id" />

需要是

<input type="hidden" name="id" id="id" value="someting here" />
于 2013-07-16T17:09:46.493 回答