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

使用JQuery从Ajax函数的WebMethod返回下拉项的简单值,jquery,ajax,webmethod,Jquery,Ajax,Webmethod,我在aspx页面上有两个下拉列表DDL1、ddl2和一个按钮btn。当我点击按钮时,需要得到显示两个下拉列表中所选项目值的警报框。我可以知道如何从web方法中做到这一点吗 按钮: <input id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' /> function showingddlva

我在aspx页面上有两个下拉列表DDL1、ddl2和一个按钮btn。当我点击按钮时,需要得到显示两个下拉列表中所选项目值的警报框。我可以知道如何从web方法中做到这一点吗

按钮:

<input  id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' />
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);
}​
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]
public static string showingddlvalues(string value)
{
    return String.Format("{0}", value);
}
jQuery:

<input  id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' />
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);
}​
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]
public static string showingddlvalues(string value)
{
    return String.Format("{0}", value);
}
请纠正我的错误。

试试看

var js=$('#idOfDrop选项:选中')。text()

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

警报(js)


您将从下拉列表中获得所选值,修改jQuery&web方法如下,然后再次检查结果:

jQuery:

<input  id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' />
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);
}​
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]
public static string showingddlvalues(string value)
{
    return String.Format("{0}", value);
}
WebMethod:

<input  id="Test" type="button" value="clickme" onclick='<%# String.Format("showingddlvalues(\"{0}\")",Eval("ddl1ID")) %>' />
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);
}​
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]
public static string showingddlvalues(string value)
{
    return String.Format("{0}", value);
}

嗨,阿比,谢谢你的快速回复,我已经粘贴了我的代码。请你指导我如何获得这两个下拉项的值,请。。。