简体   繁体   English

将2个参数传递给jQuery post

[英]Pass 2 parameters to jQuery post

I have this function: 我有这个功能:

function PostData(idTag, nameTag){
    $.post(".../info/act/set?params=" + idTag + nameTag,  function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
    })
}

In HTML i got this list with 2 inputs: 在HTML中,我得到了具有2个输入的列表:

        <li><label>NAME: <input id="name"></input></label></li>
        <li><label>ID: <input id="ident"></input></label></li>

What I'm trying to do is to pass the input values(#name and #ident contents) to the PostData() function in order to submit data to the server. 我想做的是将输入值(#name和#ident内容)传递给PostData()函数,以便将数据提交给服务器。 I have tried this but when i check the logs it shows this: "params : "[object Object][object Object]" 我已经尝试过了,但是当我检查日志时显示如下:“ params:” [object Object] [object Object]“

function submitData() {
    var name = $("#name").text();
    var loc = $("#loc").text();

    val1=$("#name").val(name);
    val2=$("#loc").val(loc);

    PostData(val1, val2);
}

Any ideas? 有任何想法吗?

Well if am not wrong you are trying to read data It will be like this 好吧,如果您输入的数据没有错,它将像这样

function submitData() {
    var val1=$("#name").val();
    var val2=$("#loc").val();
    PostData(val1, val2);
}

There are several issues to address here. 这里有几个问题要解决。 Firstly the HTML; 首先是HTML; <input /> elements do not have a closing tag so that should be removed. <input />元素没有结束标记,因此应将其删除。

In your submitData() funciton you're getting the text() of the input elements, which will be empty, then setting that to their value, before attempting to pass jQuery objects to PostData() . 在您的submitData()您将获取input元素的text() ,该元素将为空,然后将其设置为它们的值,然后再尝试将jQuery对象传递给PostData()

PostData() itself then seems to be expecting strings, not the jQuery objects that you are sending. 然后, PostData()本身似乎期望使用字符串,而不是所发送的jQuery对象。 It would also make sense to separate the values, either as a comma delimited string in a single parameter, or two separate parameters. 分隔值也很有意义,可以是单个参数中的逗号分隔字符串,也可以是两个单独的参数。 Here is a full implementation of how to achieve the latter: 这是如何实现后者的完整实现:

 function PostData(idTag, nameTag) { $.post('.../info/act/set?id=' + idTag + '&name=' + nameTag, function(data, status) { console.log(data, status); }) } function submitData() { PostData($("#name").val(), $("#loc").val()); } 
 <li> <label> NAME: <input id="name" /> </label> </li> <li> <label> ID: <input id="ident" /> </label> </li> 

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

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