简体   繁体   English

$ .ajax()给出[object HTMLInputElement]而不是值

[英]$.ajax() gives [object HTMLInputElement] instead of value

I have an $.ajax() request working properly and it's calling 3 ids. 我有一个$ .ajax()请求正常工作,它正在调用3个id。 The only id with no issues is the name id. 没有问题的唯一ID是名称ID。 The clientId logs well on the console, but prints undefined on the table. clientId在控制台上记录良好,但在表上未打印。 The url is printing [object HTMLInputElement] on the table and on the console, logs like this <input class="form-control no-border" type="text" id="redirectUrl" name="url" placeholder="http://stuffpage/redirect/?id=1" required=""> .I want to print the url that i am introducing on the input. url在表和控制台上打印[object HTMLInputElement],像这样的日志<input class="form-control no-border" type="text" id="redirectUrl" name="url" placeholder="http://stuffpage/redirect/?id=1" required=""> 。我要在输入中打印要介绍的网址。

Can you tell me what is the problem? 你能告诉我是什么问题吗?

 $('#saveButton').on('click', function() { var url = "http://stuffpage.com/redirect/redirect"; var name = $('#name').val(); console.log("name", name); var clientId = $('#clientId').val(); console.log("clicentId", clientId); var redirecUrl = $('#redirectUrl').val(); console.log("redirectUrl", redirectUrl); var formData = new FormData(); formData.append('name', name); formData.append('client_id', clientId); formData.append('url', redirectUrl); console.log('test') $.ajax({ url: url + "/saveRedirect", type: "POST", dataType: "json", data: formData, contentType: false, cache: false, processData: false, success: function(obj) { var name, clientId, redirecUrl; var rows = ''; for (i = 0; i < obj.length; i++) { rows += "<tr class='lightgrey'><th>" + obj[i].name + "</th><td>" + obj[i].clientId + "</td><td>" + obj[i].url + "</td><td><img id='editButton' class='col-md-2 edit nopad' src='http://stuffpage.com/redirect/public/img/edit.svg'><img class='col-md-2 link nopad float-right' src='http://stuffpage.com/redirect/public/img/copy.svg'></td></td></tr>"; $("#table").append(rows); console.log('sucess!'); } }, error: function() { console.log('error'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="col-md-12 marg-t-30 nopad"> <!--REGISTER NAME CLIENT ID URL--> <div class="col-md-1 nopad"> <div class="rtitle"> <div class="roboto-condensed">name</div> </div> </div> <div class="col-md-3 nopad" style="margin-right: 20px;"> <input class="form-control no-border" id="name" type="text" name="nameClient" placeholder="Insert name..." required=""> </div> <div class="col-md-1 nopad"> <div class="rtitle"> <div class="roboto-condensed">client id</div> </div> </div> <div class="col-md-3 nopad"> <select class="form-control no-border selectpicker" name='clientId' id='clientId' data-show-subtext="true" required=""> <?php echo $client_data;?> </select> </div> <div class="col-md-3 nopad"> <button id="saveButton" class="save float-right">SAVE</button> </div> <div class="col-md-12 nopad marg-t-20"> <div class="col-md-1 nopad"> <div class="rtitle"> <div class="roboto-condensed">url</div> </div> </div> <div class="col-md-11 nopad"> <input class="form-control no-border" type="text" id="redirectUrl" name="url" placeholder="http://stuffpage/redirect/?id=1" required="" value=""> </div> </div> </div> <!--col-md-12--> 

Looks like you have a typo: 看起来您有错别字:

var redirecUrl = $('#redirectUrl').val();

should be 应该

var redirectUrl = $('#redirectUrl').val();

You can replace 您可以更换

var clientId = $('#clientId').val();

To

var clientId = $('#clientId option:selected').val();

And

var redirectUrl = $('#redirectUrl').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM