首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用一个ajax函数更改两个html id

使用一个ajax函数更改两个html id
EN

Stack Overflow用户
提问于 2013-02-22 05:36:18
回答 1查看 434关注 0票数 0

我需要在我的ajax请求中添加一些东西,以显示它从<span id="total_vote_down_<?php echo $mes_id2; ?>">中减1。现在,我的ajax只是将<span id="total_vote_up_<?php echo $mes_id1; ?>">的值加1,我需要它在同一函数中从<span id="total_vote_up_<?php echo $mes_id1; ?>">中减去1。我知道我的代码很马虎……听我说..。

顺便说一句,我知道ajax实际上并没有增加和减少任何东西,它只是为客户演示而已,只是不知道用什么更好的方式来表达我的问题

general.js

代码语言:javascript
复制
$(".vote").click(function() 
{

var id = $(this).attr("id");
var name = $(this).attr("name");
var eData = $(this).attr("data-options");
var dataString = 'id='+ id + '&' + eData ;
var parent = $(this);


if(name=='up')
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "up.php",
data: dataString,
cache: false,

success: function(data) { $('#total_' + parent.attr("id")).text(data); }
});

}
else
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "down.php",
data: dataString,
cache: false,

success: function(data) { $('#total_' + parent.attr("id")).text(data); }

});


}

});

index.php

代码语言:javascript
复制
<div id="main">
<div id="left">
<span class='up'><a title="vote_down_" id="vote_up_<?php echo $mes_id1; ?>" class="vote" name="up" data-options="key1=<?php echo $mes_id1;?>&key2=<?php echo $mes_id2;?>&key3=<?php echo $totalvotes1;?>&key4=<?php echo $totalvotes2;?>"> <img src="up.png" alt="Down" /></a></span><br />
<span id="total_vote_up_<?php echo $mes_id1; ?>"><?php echo $totalvotes1; ?></span><br />
</div>
<div id="message">
    <?php echo $message1; ?>
</div>
<div class="clearfix"></div>
</div>
<div id="main">
<div id="right">
<br />
<span id="total_vote_down_<?php echo $mes_id2; ?>"><?php echo $totalvotes2; ?></span><br />
<span class='down'><a id="vote_down_<?php echo $mes_id2; ?>" class="vote" name="down" data-options="key1=<?php echo $mes_id1;?>&key2=<?php echo $mes_id2;?>"><img src="down.png" alt="Down" /></a></span>
</div>
<div id="message">
    <?php echo $message2; ?>
</div>
<div class="clearfix"></div>
</div>

这是up.php

代码语言:javascript
复制
<?php

session_start();
include("config.php");

$ip=$_SERVER['REMOTE_ADDR']; 

$mes_id1 = $_POST['key1'];
$mes_id2 = $_POST['key2'];
$totalvotes1 = $_POST['key3'];
$totalvotes2 = $_POST['key4'];
$new_totalvotes1 = $totalvotes1 + 1;
$new_totalvotes2 = $totalvotes2 - 1;

$ip_sql=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id1' and ip_add='$ip'");
$count=mysql_num_rows($ip_sql);

$ip_sql2=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id2' and ip_add='$ip'");
$count2=mysql_num_rows($ip_sql2);


// if the user has already voted, execute script
if($count==0 && $count2!=0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);

$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);

$sql = "update Messages set totalvotes=totalvotes-1  where mes_id='$mes_id2'";
mysql_query( $sql);

$sql_in = "DELETE FROM Voting_IP WHERE mes_id_fk='$mes_id2'";
mysql_query( $sql_in);

echo $new_totalvotes1;

// if the user has not voted, execute script
}
else if($count==0 && count2==0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);

$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);

echo $new_totalvotes1;

}
?>

down.php与up相同,只是取值相反

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-22 21:46:43

如果在PHP中创建关联数组,则可以使用json_encode将其转换为JSON。如下所示:

代码语言:javascript
复制
// obviously this will be constructed dynamically from your database
$d = array('Will' => 10, 'Henry' => 12);
// echo as json
echo json_encode($d);

然后在你的JavaScript中:

代码语言:javascript
复制
$.ajax({
    type: "POST",
    url: "down.php",
    data: dataString,
    cache: false,
    dataType: "json",

    success: function(data) { 
        // here data will look like { "Will":10, "Henry": 20 };
        // I'm going to assume `total_will` is the total votes for william
        var candidate;
        for (candidate in data) {
            $("#total_" + candidate).text(data[candidate]);
        }
        // now #total_Will has the text 10
        // and #total_Henry has the text 20
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15012675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档