【问题标题】:Passing data variables from PHP to jquery ui dialog将数据变量从 PHP 传递到 jquery ui 对话框
【发布时间】:2015-10-18 21:13:04
【问题描述】:

我有这段 php 代码:

    <?php
$posts = new Posts();

foreach($posts->getPosts() as $post){ ?>
    <div class="post">
        <h3><a class="post-link" data-post-id="<?php echo $post['id']; ?>" href="javascript:void(0)"><?php echo $post['title']; ?></a></h3>
    </div>

<?php } ?>

<div id="insert-answer" title="Add new idea to post">
<form id="myForm" action="insertidea.php" method="post">
    <fieldset>
        <p><label for="idea">Your idea:</label>
            <input type="text" name="idea" class="idea"</p>
        <p><label for="pic">Have a pic? Paste its URL here! (optional)</label>
            <input type="text" name="pic" class="pic"></p>
        <input type="hidden"class="author" name="author" value="<?php echo $_SESSION['google_data']['id']; ?>" />
        <input type="hidden"class="forpost" name="forpost" value="<?php echo $post['id']; ?>" />
    </fieldset>
</form>

我想将 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 脚本。

【问题讨论】:

    标签: javascript php jquery jquery-ui


    【解决方案1】:

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

    $( ".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() { 部分:

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

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

    更新代码:@charlietfl 完全正确,var post-id 不正确,我已经编辑了变量的名称。

    【讨论】:

    • 我在声明 post-id 时遇到了某种语法错误(缺少分号?)我还在测试这个,非常感谢!
    • 这就是我得到的: SyntaxError: missing ;声明前 var post-id = $(this).data("post-id");编辑:它引用了函数中的 var post-id 行,phpstorm 也在抱怨它。
    • var post-id 是无效的 javascript 变量语法,会抛出错误
    • @charlietfl:你说得对,我太累了,暂时离开 :-) 我已经编辑了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多