2013-07-16 38 views
0

我们HTML元素,这是我的网页看起来像:传递数据使用jQuery

. 
. 
. 
<?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”是我在这里使用的对话框。

所以,现在我觉得它更容易寻找错误。请请帮助我。这让我疯狂。

+0

对不起,这还不行。我尝试了所有的想法,但结果仍然相同。帮我。 – user2387319

+0

我编辑了这个问题,请尝试再次帮助我。 – user2387319

回答

0

你的HTML表单输入字段id =“id”没有值,所以你不能期望得到一个。

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

必须

<input type="hidden" name="id" id="id" value="someting here" /> 
+1

我认为关键是他希望使用JQuery动态地执行此操作。即单击按钮时设置隐藏字段的值。 – Katana314

+1

用户在点击按钮时添加了一个名为value的属性,为什么他/她不希望得到一个值? – user1

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> 

这对我的作品。有几件事要看:ids应该从一个字母开始,而不是一个数字。您的功能与您的输入名称相同。我看到以下错误:TypeError:'[object HTMLInputElement]'不是一个函数(评估'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> 
+1

+1我认为就是这样。另外,不需要'$(“#edit #edit_form #id”)或'$(“#edit #edit_form label #new_comment”)''。由于ID应该是唯一的,所以使用'$(“#id”)'和$(“#new_comment”)'会产生同样的效果。 – acdcjunior

+0

其实我在这里使用了一个数组,所以有几个这样的链接,这实际上是:<?php echo $ array [$ i];?>。这是行得通的,我从网页的源头看到这个函数正确地获取了值。之后不知道发生了什么。 @Barbara Laid – user2387319

+1

你的edit_comment参数还没有引号。你有没有在edit_comment中试过console.log的id和comment?没有引号,javascript会认为它们是变量名称,它们的值将是未定义的。 –

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。

+0

试过了,也没有工作。@ adeneo – user2387319

1
function edit_comment(id,comment){ 
    $("#edit_form #id").attr("value",id); 
    $("#edit_form #new_comment").attr("value",comment); 
}; 

我除去#edit从选择器,因为#edit_form不是内部#edit。而且label是不必要的。

+1

一般来说''edit_form'也是不需要的,因为ID是唯一的。 – adeneo

+1

是的,true..'#id'和'#new_comment'应该就足够了。 – Pratheep

+0

是的,我也只使用那些ID,没有别的,但仍然是相同的结果。 – user2387319