使用JQuery从Ajax函数中使用WebMethod的下拉项的简单返回值

时间:2012-12-07 10:46:00

标签: jquery ajax webmethod

我在aspx页面上有两个dropdownnsddl1,ddl2和一个按钮btn。当我点击按钮时需要获取警告框,显示两个下拉列表中所选项目的值。我知道如何通过网络方法做到这一点,请...

按钮:

<input  id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' />

网络方法:

[的WebMethod]

    public static string showingddlvalues(string ddl1ID)
    {
        return String.Format("showing selected ddl1ID{0} value here ", ddl1ID);
    }

jQuery的:

function showingddlvalues(ddl1ID) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/showingddlvalues",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{ddl1ID:'" + ddl1ID + "'}",
        success: AjaxSucceeded,
        error: AjaxFailed
    });
}

function AjaxSucceeded(result) {
    alert(result.d);
}

function AjaxFailed(result) {
    alert('error:' + result.status + ' ' + result.statusText);
}​

请在我出错的地方纠正我。

2 个答案:

答案 0 :(得分:1)

修改你的jQuery&amp;像这样的网络方法并再次检查结果:

<强> jQuery的:

function showingddlvalues(ddl1ID) {

    var value = $('#' + ddl1ID).val();

    $.ajax({
        type: "POST",
        url: "Default.aspx/showingddlvalues",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{value:'" + value + "'}",
        success: AjaxSucceeded,
        error: AjaxFailed
    });
}​

<强>的WebMethod:

[WebMethod]
public static string showingddlvalues(string value)
{
    return String.Format("{0}", value);
}

答案 1 :(得分:0)

试,

var js = $('#idOfDrop option:selected')。text();

或者

var js = $(“[name = nameOfSelect]:selected”)

警报(JS);

您将从下拉列表中获取所选值