【问题标题】:data-attribute wont submit content数据属性不会提交内容
【发布时间】:2014-09-11 07:17:49
【问题描述】:

我正忙于开发我的第一个 Twitter 应用程序。但是,由于某种原因,我陷入了一些我无法解决的基本问题。我曾经经常使用数据属性,但我无法让它工作。它只是不会提交我的数据属性的内容

我的 HTML 的一部分。

<div id="invTweet" class="_invitation-tweet-inner no-select" data-tweet="Here comes the content of the tweet which will be submitted">Here comes the content of the tweet</div>

<div id="post-invitation-tweet" class="button blue post-tweet">Post Tweet</div>

我的 Javascript 的一部分

    var tweet = $("#invTweet").attr("data-tweet");      
    $(document).on('click',"#post-invitation-tweet",function(e) {
    e.preventDefault();
        $.ajax({
            type: "POST",
            url: "../includes/db-requests/db-postInvitationTweet.php",
            data: {tweet:tweet},
            success: function(data){    finishInvitationTweet(data);    }
        });
    });

function finishInvitationTweet(data) {
    alert(data);
}

我使用 alert(data) 进行调试。当我单击 Post Tweet 按钮时,将打开一个带有回显“未定义索引:tweet emptyTweet”的警报。在下面查看我的 db-postInvitationTweet.php 的 sn-p:

//Get Tweet data
$tweetMsg = safe($mysqli,$_POST['tweet']);

//Check if $tweetMsg is empty. No > post tweet.
if ($tweetMsg != '') {

$post_tweet = $connection->post('statuses/update', array('status' => $tweetMsg));

if (187 == $connection->http_code) {
  echo "statusDuplicate";
  die();
}

if (!$post_tweet) {
    echo "tweetError";  
} else {
    //Update the invitation_sent column in database
    $stmt = $mysqli->prepare("UPDATE members SET invitation_tweet = '1' WHERE oauth = ? ") or die (mysqli_error($mysqli));
    $stmt->bind_param('s', $access_token['oauth_token']);
    $stmt->execute();
    $stmt->close();
    echo "tweetSuccess";
}

} else {
    echo "emptyTweet";  
}

我错过了什么:')

【问题讨论】:

  • @RobSchmuecker 感谢您的关注。我在写这篇文章时更改了来源。我刚刚更新了我的帖子。它正在提醒“emptyTweet”,它可以在我的 db-postInvitationTweet.php 的底部找到。当我定义它时,它不会以某种方式发布推文的内容..
  • console.log(tweet)alert(tweet) show just after e.preventDefault()` 是什么意思?
  • 在问这个问题之前应该执行一些非常基本的调试步骤

标签: javascript php jquery twitter


【解决方案1】:

您使用的是什么版本的 jQuery?从 1.4 版开始,访问数据属性的适当方法是通过 data() 方法,请参阅方法签名和文档here

如果你改变它,你也许可以让它正常工作。请参阅 jsFiddle 并使用可行的解决方案作为概念证明。

改变了

var tweet = $("#invTweet").attr("data-tweet");

var tweet = $("#invTweet").data("tweet");

【讨论】:

  • 两者都适用于字符串。这与读取任何其他属性没有什么不同,除了 data() 将返回对象和数组(如果适用)
  • @Dogoferis 我使用的是 1.10.2。无论如何,我已将其更改为 .data 并且现在可以正常工作。不过有点奇怪,因为它昨天工作得很好……谢谢!我很快就会接受你的回答!
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 2019-06-20
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多