将数据变量从PHP传递到jquery ui对话框

我有这个PHP代码:

getPosts() as $post){ ?> 

<a class="post-link" data-post-id="" href="javascript:void(0)">

<input type="text" name="idea" class="idea"

<input type="hidden"class="author" name="author" value="" /> <input type="hidden"class="forpost" name="forpost" value="" />

我想将post id数据变量传递给jquery ui对话框:

  $( "#insert-answer" ).dialog({ autoOpen: false, modal:true, buttons: { "Add idea": function() { var forpost = $(this).data("post-id"), // HOW CAN I GET THIS??? author = $(".author").val(), idea = $(".idea").val(), pic = $(".pic").val(); $.post('insertidea.php',{ forpost: forpost, author: author, idea: idea, pic: pic, action:'joined' });//End Post $(".forpost").val(''); $(".author").val(''); $(".idea").val(''); $(".pic").val(''); $(this).dialog("close"); }, Cancel: function() { $( this ).dialog( "close" ); } } }); $( ".post-link" ).click(function() { $( "#insert-answer" ).dialog( "open" ); }); 

问题是我主要是一个php女孩,我不太了解我已经检查过的百万个JS例子。 我想要做的是使用post id作为来自php的变量,我可以在我的对话框中使用它通过post发送到另一个php脚本。

您可以按如下方式更改后链接function:

 $( ".post-link" ).on('click', function() { var postid = $(this).data("post-id"); var answer = $("#insert-answer"); $(answer).data('post-id', postid); $(answer).dialog( "open" ); }); 

然后抓住"Add idea": function() { part with:

 var forpost = $("#insert-answer").data("post-id"), author = $(".author").val(), idea = $(".idea").val(), pic = $(".pic").val(); 

所以你所做的基本上是将当前的post-id保存到#insert-answer并从对话框中获取它。 这是你追求的吗?

更新的代码 :@charlietfl是绝对正确的, var post-id不正确,我编辑了变量的名称。