How to create polling postoverflow post as jquery / ajax function?

Is it possible to use jQuery to call an action and then change the image when it is successful, just like the postoffflow postoffflow function?

In my opinion, I am using the following code, but I do not want to update the browser. Can someone provide me with a code for this?

Many thanks.

<%if (!item.IsPrinted)
{ %>
     <%=Html.ImageLink("~/Content/images/web/delete.png", "printed", "MarkAsPrinted", "Order", item.TaskID, null, null)%>
 <%}
 else
  {%>
       <img src="~/Content/images/web/star.png" alt="printed" />                    
  <% }  %>
+3
source share
5 answers

, ajax- , ajax. (, ..), ajax

Edit:

public string UpdateVoteScore(int postId, int value) {
     // store value to database

     return "success";
}

JavaScript:

var UpdateScore = function(postId, newValue) {
   $.ajax({
           type: "POST",
           url: /YourController/UpdateVoteScore,
           data: { postId: postId, value: newValue },
           success: function(result) {
              // replace your image
              $("#MyImage" + postId).attr("src", "some new image path here");
           },
           error: function(req, status, error) {
           }
    });
}

:

<img id='<%= "MyImage" + post.Id %>' 
     src="some image path"
     onclick="UpdateScore(scoreValueHere);"></img>

: post , , post.Id , ,

+4

, JQuery . , JQuery HTML- , script.

:

<html>
  <head>
    <script type="text/javascript" src="jquery-1.3.1.js"></script>
    <script type="text/javascript" src="jquery.form.js"></script>
    <script>
      $(document).ready(function(){
        $('#response a').bind('click',function(event){
          event.preventDefault();
          $.get(this.href,{},function(response){
          if (response.indexOf("OK") >= 0) {
            $('#response').html("<img src="~/Content/images/web/star.png" alt="printed" /> ");
          }
          })    
        })
      });
    </script>
  </head>
  <body>
    ... the other things on you're page
    <div id="response">
      <%=Html.ImageLink("~/Content/images/web/delete.png", "printed", "MarkAsPrinted", "Order", item.TaskID, null, null)%>
    </div>
    ... more things on you're page...
  </body>
</html>

, script "", , div "respone".

+2

, , ajax.

, , :

[browser request]->[server handles request]->[new page sent to browser]

ajax , , javascript, - . javascript script , .

, "vote.asp" - , javascript .

0

, - :

$('#vote_button').click(function() {
   $.ajax({
      type: "GET",
      url: "script_name.asp", //your script that talks to the database
      data: "id=" + some_var, //get arguments
      success: $("#image_to_be_changed").attr("src", "some new image path here");
});
0

I created a simple voting application similar to stackoverflow.com using angularjs, php and mysql with download code. You just need to change 2 php files to ASP.NET files. Everything else will be intact. Creating a voting application using AngularJS is much simpler and more flexible.

0
source

Source: https://habr.com/ru/post/1741605/


All Articles