0

我想更新当用户点击赞按钮时增加的赞数。就像在我的网站上一样,当用户在此页面 http://modelspk.com/urwa-tul-wusqa-model18 上单击“赞”按钮时,数据库中的赞数增加了 1。但是直到页面没有刷新,前端才更新。我目前使用的代码如下

<script type="text/javascript">
    $("#likeThis").live("click", function () {
        var pid = document.getElementById("pid");
        var uRL = "/updateRatings.aspx?opt=1&pid=" + pid.value;
        $.post(uRL);
        alert('You Like This');
        this.disabled = true;
        document.getElementById("dislikeThis").disabled = true;
        return false;
    });

    $("#dislikeThis").live("click", function () {
        var pid = document.getElementById("pid");
        var uRL = "/updateRatings.aspx?opt=2&pid=" + pid.value;
        $.post(uRL);
        alert("You don't Like This");
        this.disabled = true;
        document.getElementById("likeThis").disabled = true;
        return false;
    });         

</script>

喜欢或不喜欢的html输入按钮如下..

 <tr>
                    <td>
                        <asp:HiddenField ID="newPhotoID" runat="server" />
                        <input type="hidden" id="pid" value="<%=ModelID%>" />
                        <asp:Label ID="lblModelID" runat="server" Visible="false"></asp:Label>
                        <table width="253" align="center">
                            <tr>
                                <td align="left" class="txt" style="color: #0068c6; font-weight: bold;">
                                    Rate This:
                                </td>
                                <td align="center" class="WaterText">
                                    <input type="image" id="likeThis" onclick="javascript:rateThis(1,<%=ModelID%>);"
                                        src="/images2/inact-thumb.jpg" onmouseover="this.src='/images2/active-thumb.jpg'"
                                        onmouseout="this.src='/images2/inact-thumb.jpg'" />&nbsp;<%=Likes%>
                                    <span class="txt" style="color: #0068c6; font-weight: bold;">Likes</span>
                                </td>
                                <td align="center" class="WaterText">
                                    <input type="image" id="dislikeThis" src="/images2/no-inact.jpg" onclick="javascript:rateThis(2,<%=ModelID%>)"
                                        onmouseover="this.src='/images2/no-active.jpg'" onmouseout="this.src='/images2/no-inact.jpg'" />&nbsp;<%=Dislikes%>
                                    <span class="txt" style="color: #0068c6; font-weight: bold;">Disikes</span>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td width="127" align="right" class="WaterText">
                        <%=Views %>
                        <span class="txt" style="color: #0068c6; font-weight: bold;">Views</span>
                    </td>
                </tr>

我想要做的是当用户点击like按钮时,它应该同时增加一个而不刷新页面。

问候:穆达西尔

4

2 回答 2

0

自从 。不推荐使用live ()尝试使用 . on () 喜欢

$("#likeThis").on("click", function () {

并将这些功能放在 document.ready 中

$(document).ready(function(){
     $("#likeThis").on("click", function () {
     ---------------------
});
于 2013-06-10T10:20:59.843 回答
0

您应该手动更新编号。使用 Javascript 或 jQuery 的好恶。

//added new argument, pass image for further use
<input type="image" id="likeThis" onclick="javascript:rateThis(1,<%=ModelID%>,this);" 

src="/images2/inact-thumb.jpg" ;<%=赞%>

//do not use live.. use simple javascript

function rateThis(1,modelId,image){
    $.ajax({
        url:"/updateRatings.aspx?opt=1&pid=" + pid.value,
        success:function(result){
            $(image).siblings("span").text(result.d);//update the no of likes and dislikes
        }
    })
}

asp.net 在 Page_Load 中返回数据

您应该参考上面的链接,了解如何在使用页面而不是 Web 服务时从页面获取 json 数据。

于 2013-06-10T10:57:50.093 回答