通过Javascript、How将Dropdownlist索引作为参数发送给WS

send Dropdownlist index as a parameter to WS thru Javascript, How?

本文关键字:参数 WS 索引 Javascript How Dropdownlist 通过      更新时间:2023-09-26

我正试图用Jquery Autocomplete:向我的WS发送两个参数

  1. 文本框,我想要完成的位置
  2. 下拉列表索引

我在获取dropdownlist索引时遇到了问题,因为我只获取Controller的名称。

这是我的脚本:

<script type="text/javascript" language="javascript">
    $(function() {
        $('#<%= TextBoxes1.ClientID%>').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "WB/EmployeeService.asmx/GetEmpolyeeId",
                    data: "{ 'Text': '" + request.term + "','SelectedIndex':'" + '#<%= DP1.ClientID %>' + "'}",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {
                        response(result.d);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            minLength: 0
        });
    });
</script>

这是我的WS-

public List<string> GetEmpolyeeId(string Text, string SelectedIndex)

我需要做些什么才能让它发挥作用?

您需要下面的2,正如您在问题中提到的那样。

  1. 文本框,我想要完成的地方--request.term,您使用它是正确的
  2. dropdownlist索引——假设dropdownlist-id是DP1,您需要使用以下方法获取其索引

$("#<%= DP1.ClientID %>")[0].selectedIndex

所以把所有的东西放在一条线上,

data: "{ 'Text': '" + request.term + "','SelectedIndex':'" + $("#<%= DP1.ClientID %>")[0].selectedIndex + "'}",

示例

function OnChangeVal() {
  alert($("#sel")[0].selectedIndex);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select onchange="OnChangeVal()" id="sel">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

我搞定了!!:-)

这就是我所做的:

<script type="text/javascript" language="javascript">
    var ddl = document.getElementById('<%=DP1.ClientID%>');
    $(function () {
        $('#<%= TextBoxes1.ClientID%>').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "WB/EmployeeService.asmx/GetEmployeeDetails",
                    data: "{ 'Text': '" + request.term + "','SelectedIndex':'" + ddl.selectedIndex  <%--'#<%= DP1.ClientID %>'--%> + "'}",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    success: function (result) {
                        response(result.d);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            minLength: 0
        });
    });
</script>

我在"data"(Dropdownlist索引)中添加了一个变量,而我在进入函数之前定义了Dropdownlist。

WS也遇到了同样的问题。