【问题标题】:send Dropdownlist index as a parameter to WS thru Javascript, How?通过 Javascript 将 Dropdownlist 索引作为参数发送到 WS,如何?
【发布时间】:2014-10-26 12:40:03
【问题描述】:

我正在尝试使用 Jquery Autocomplete 向我的 WS 发送 2 个参数:

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

我在获取下拉列表索引时遇到了问题,因为我只得到了控制器的名称。

这是我的脚本:

<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)

我需要做什么才能让它工作?

【问题讨论】:

    标签: javascript c# jquery asp.net web-services


    【解决方案1】:

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

    1. 文本框,我要填写的地方——request.term,你用对了。
    2. dropdownlist index -- 假设dropdownlist id是DP1,你需要使用下面的方法获取它的索引

    $("#&lt;%= DP1.ClientID %&gt;")[0].selectedIndex

    所以把所有东西放在一条线上,就是这样,

    data: "{ 'Text': '" + request.term + "','SelectedIndex':'" + $("#&lt;%= DP1.ClientID %&gt;")[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>

    【讨论】:

      【解决方案2】:

      我明白了!!! :-)

      这就是我所做的:

      <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>
      

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

      WS 风格相同。

      【讨论】:

      • 我不能在我的答案上加上 V,因为我没有权限。如果有人会这样做,我将不胜感激。谢谢
      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多