如何将文本框值传递给jQuery .ajax方法? [英] How do I pass a textbox value to jQuery .ajax method?

查看:352
本文介绍了如何将文本框值传递给jQuery .ajax方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的jquery方法:

Here's my jquery method:

$.ajax({
    type: "GET",
    url: "Home/GetSocialMentionBlogs/"
    dataType: "json",
    success: function (data) {
        var obj = JSON.parse(data);
        $.each(obj.items, function (i, item) {
            $("<strong><p>" + item.title + "</strong></p>").appendTo("#blogs");
            if (i == 5) return false;
        });
    }
});

我要做的是当用户单击按钮,调用其方法并传递文本框的值时,以使URL现在为:

What I want to do is when a user clicks a button, callt his method, and pass in the value of a textbox so that the URL will now be:

url: Home/GetSocialMentionBlogs/value from text box

我当然需要对此进行URL编码,但是到目前为止,我还不知道如何将值传递给此.ajax函数.

Of course I'll need to URL encode that, but as of now, I don't know how to pass in values to this .ajax function.

我对jQuery和MVC完全陌生,因此请原谅我迄今为止的无知.

I'm completely new to jQuery and MVC so pardon my ignorrance ont he subject so far.

推荐答案

如果输入字段具有您要执行的"id"值

Well if the input field has an "id" value you'd just do

url: "Home/GetSocialMentionBlogs/" + $('#inputFieldId').val(),

如果只有名字,那么您可以这样做:

If all you've got is the name, then you could do:

url: "Home/GetSocialMentionBlogs/" + $('input[name=inputFieldName]').val(),

是您需要做的还是我缺少一些细节?

Is that all you need to do or am I missing some detail?

哦,要进行URL编码,只需使用Javascript encodeURIComponent()函数.

Oh and to URL encode just use the Javascript encodeURIComponent() function.

这篇关于如何将文本框值传递给jQuery .ajax方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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