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 (evaluating '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
function edit_comment(id,comment){
    $("#edit_form #id").attr("value",id);
    $("#edit_form #new_comment").attr("value",comment);
};

が内部にない#editため、セレクターから削除しました。そして、は不要です。#edit_form#editlabel

于 2013-07-16T17:19:22.793 に答える
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 に答える