Home Web Front-end JS Tutorial jQuery implements simple like effect_jquery

jQuery implements simple like effect_jquery

May 16, 2016 pm 03:21 PM
jquery like

The example in this article explains the detailed code of jQuery to achieve a simple like effect. The specific content is as follows

Rendering:

The following provides an example code for "like", implemented with ASP.NET MVC4+jQuery Ajax.
Model:

namespace MvcAjaxAdd.Models 
{ 
 public class ClickCountModel 
 { 
  [Key] 
  [DatabaseGeneratedAttribute(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
  public int ID { get; set; } 
 
  public string URL { get; set; } 
 
  public int? Num { get; set; } 
 } 
} 
Copy after login

View:

@{ 
 ViewBag.Title = "Index"; 
} 
@model MvcAjaxAdd.Models.ClickCountModel 
<script src="~/Scripts/jquery-1.7.1.js" type="text/javascript"></script> 
<script type="text/javascript"> 
 $(function () { 
  var obj = { 
   "num": $("#lblnum").text(), 
   "url": window.location.pathname//获取/Home/Index 
  }; 
  $("#addnum").click(function () { 
   $.ajax({ 
    type: 'POST', 
    url: '/Home/ClickGood', 
    data: obj, 
    success: function (data) { 
     $("#lblnum").text(data.Num); 
     //其它操作,比如每个登录用户只能点一次,按钮禁用等 
    } 
   }); 
  }); 
 }); 
</script> 
<div id="addnum" style="width: 70px; height: 70px; background-color: #FF9900"> 
 <div align="center" style="margin-top: 10px;"> 
  <label style="color: White; font-size: 20pt;"> 
   顶</label></div> 
 <div align="center"> 
  <label id="lblnum" style="color: White; font-size: 10pt;"> 
   @Model.Num</label></div> 
</div> 
Copy after login

Controller:

namespace MvcAjaxAdd.Controllers 
{ 
 public class HomeController : Controller 
 { 
  private ClickCountContext db = new ClickCountContext(); 
 
  public ActionResult Index() 
  { 
   ClickCountModel ClickCountModel = db.ClickCountModels.FirstOrDefault(x => x.URL == "/"); 
   return View(ClickCountModel); 
  } 
 
  [HttpPost] 
  public JsonResult ClickGood(ClickCountModel ClickCountModel) 
  { 
   ClickCountModel newClickCountModel = db.ClickCountModels.FirstOrDefault(x => x.URL == ClickCountModel.URL); 
   newClickCountModel.Num++;//数量+1 
   db.SaveChanges(); 
   return Json(newClickCountModel); 
  } 
 } 
} 
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone in learning jquery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between likes and love on a WeChat video account? What is the use? What is the difference between likes and love on a WeChat video account? What is the use? Mar 26, 2024 pm 12:16 PM

In WeChat video accounts, likes and love are common ways for users to interact. Likes indicate the user's recognition and support for the video content they like, while hearts show the user's love and concern for the video creator. Although likes and hearts both express the user's recognition and love for the video content, there are some subtle differences in substance. 1. What is the difference between likes and love on a WeChat video account? Likes are often a way of expressing general approval and support, with users simply clicking the &quot;Like&quot; button under a video to express their approval of the video. This simple and direct format allows users to express their opinions quickly, while also helping the video spread more widely. Love is personal and emotional. In the WeChat video account, users can click the &quot;

Where can I read the comments liked by Xiaohongshu? What rules do you need to pay attention to on the Xiaohongshu platform? Where can I read the comments liked by Xiaohongshu? What rules do you need to pay attention to on the Xiaohongshu platform? Mar 30, 2024 pm 06:51 PM

In today's era of information explosion, social media has become an indispensable part of people's daily lives. As the leading lifestyle sharing platform in China, Xiaohongshu has attracted a large number of users to share and obtain information here. So, how do we view like comments on the Xiaohongshu platform? This article will answer this question in detail. 1. Where can I read the comments liked by Xiaohongshu? First, we need to log in to our account on the Xiaohongshu platform. After logging in, we can see the content recommended on the homepage and the updates posted by people we follow. Below each post, we can see buttons for like and comment. Click the Like button to express your approval of this content. Click the comment button to view other users' comments on the content. 3.If

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

What do you think about other people's comments that Xiaohongshu has liked? Which types of content are more likely to get likes and comments? What do you think about other people's comments that Xiaohongshu has liked? Which types of content are more likely to get likes and comments? Mar 30, 2024 pm 08:06 PM

In the digital age, Xiaohongshu has become an important platform for people to share their lives and exchange experiences. Users interact through likes and comments, forming a tight social network. This article will explore how to view comments that have been liked by others on Xiaohongshu, and what types of content are more likely to get likes and comments. 1. What do you think about other people’s comments that Xiaohongshu has liked? Users can interact by viewing liked comments to express support and liking. The specific viewing method is as follows: 1. On the Xiaohongshu homepage, click the "Discover" button to enter the "Discover" page. 2. On the "Discover" page, click the "Circle" button to enter the "Circle" page. 3. On the "Circles" page, browse the topics or circles you are interested in and find comments you like. 4. Click on the comment to see the likes

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles