如何将HTML输入值作为字符串放在Jquery对象值中?

时间:2016-05-20 17:38:50

标签: jquery html json ajax

$("#btn_edit").click(function(){
    var token = localStorage.getItem('id_token');    
    var obj = {};
    obj.subject = $("input[name=subject]").val();
    obj.description = $("textarea[name=description]").val();

    $.ajax({
       type: "PUT", 
       url: "http://myurl", 
       contentType: "application/json",
       dataType: "json",
       data: JSON.stringify(obj),
       success: function(jsonresponse) {
           $(document.body).append(JSON.parse(jsonresponse));
       },
       beforeSend: function(xhr, settings) {
           xhr.setRequestHeader('Authorization', 'Bearer ' + token);
       }
    });
});

但是当手动添加值时,它可以很好地工作,甚至输入值也会打印在警报中

alert("Text: " + $("input[name=subject]").val() + " " +  $("textarea[name=description]").val());

var obj = {};
obj.subject = "Hello";
obj.description = "Help please";

0 个答案:

没有答案