具有功能的Ajax POST [英] Ajax POST with function

查看:87
本文介绍了具有功能的Ajax POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码获取功能,并且效果很好.我正在通过此功能获取数据.

I was using this code for get function and It works perfectly. Im getting data with this function.

    function getdata(getdatafrom, resultclass){
    $.get(getdatafrom, function(data) {
        $(resultclass).html(data);
    });
}

但是我需要这个作为post方法.我要使用此get方法获取输入,我必须将其发布.它必须看起来像这样.

But I need this for post method. Im getting inputs with this get method I have to post it. It has to look like this.

function postdata(postdatafrom, inputnamesvaluelist){
    $.post(postdatafrom, function(data) {
        $(resultclass).html(data);
    });
}

我将在此代码上输入输入名称,例如:

I will enter input names on this code like :

onclick="postdata(post.php,input1-input2-input3)"

它将发布此输入.我该怎么办?

And it ll post this inputs.. How can I do this?

推荐答案

如果要使用内联事件来执行此操作,则应添加单引号:

If you would do this using inline-event you should add single quotes :

onclick="postdata('post.php', '#input1-#input2-#input3')"

那么您的js应该是:

function postdata(postdatafrom, inputnamesvaluelist){
    var inputs_ids = inputnamesvaluelist.split('-');//split string passed into array of ids
    var parameters={};

    //construct obejct of {name: value,..} that you could pass it in post request
    $.each(inputs_ids, function(index, input_id){
        var input_name  = $(input_id).attr("name");
        var input_value = $(input_id).val();

        parameters[input_name]=input_value;
    })

    //post parameters to given "postdatafrom"
    $.post(postdatafrom, parameters, function(data) {
        //Your code here
    });
}

希望这会有所帮助.

这篇关于具有功能的Ajax POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆