Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm totally new to jQuery and I need to rewrite this bit of javascript to jQuery. Can you help me out? Is this correct?

Javascript:

var xmlHttpRequest = new XMLHttpRequest();

function getTasks(){
var name=document.getElementById("name").value;
var summary=document.getElementById("summary").value;
    if (xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200)
        {
            document.getElementById("open").innerHTML=xmlHttpRequest.responseText;
        }
  xmlHttpRequest.open("Post","jsontaskmanager?name=" + name + "&summary=" + summary, true);
  xmlHttpRequest.send();
}

jQuery:

var xmlhttp = new XMLHttpRequest();

$("document").ready(function(){
    var name = $("name").val();
    var summary = $("summary").val();
    //blah
    $.ajax({
        url: "jsontaskmanager",
        type: "POST",
        dataType: "json",
        data: { name: name, summary: summary },
        success: function(response) {
            $("open").html(response);
        }
    });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use $("#yourid") to get an element with ID yourid:

$(document).ready(function(){
    var name = $("#name").val();
    var summary = $("#summary").val();
    //blah
    $.ajax({
        url: "jsontaskmanager",
        type: "POST",
        dataType: "json",
        data: { name: name, summary: summary },
        success: function(response) {
            $("#open").html(response);
        }
    });
});

More info: http://www.w3schools.com/jquery/jquery_ref_selectors.asp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...