如何将客户端javascript返回值传递给ASP经典服务器端

时间:2022-06-20 04:37:19

javascript

       function test(abc) {
         var ddlArray = new Array();
            var ddl = document.getElementById('AdjusterList');
          for (i = 0; i < ddl.options.length; i++) {
            ddlArray[i] = ddl.options[i].value;
          }
  var indexsel = ddl.selectedIndex;
      return indexsel ;            
     }

ASP

                 strArrayCRN = osRecordSet.RecordCount
                 strArrayCRN2 = osRecordSet2.RecordCount
                 dim StrCount 
                 StrCount =strArrayCRN+strArrayCRN2

HTML +ASP

<select name="AdjusterList" id="AdjusterList" onchange='test("<%=StrCount%>")'><%
%>
   <option value="0">Please choose an option from the list.</option>
  <% Do While (osRecordSet.EOF = False)
       %><option value="<%=osRecordSet.RowPosition%>">
          <%=osRecordSet.Fields("NAME")%></option>
      <%
     osRecordSet.MoveNext
      Loop        %><%
    Do While (osRecordSet2.EOF = False)
            %><option value="<%=osRecordSet2.RowPosition%>">
           <%=osRecordSet2.Fields("NAME")%></option>
       <%   osRecordSet2.MoveNext    Loop        %>

Here i want to pass the return value of function test() i.e value of the selected index to asp server side

这里我想将函数test()的返回值,即所选索引的值传递给asp服务器端

1 个解决方案

#1


5  

If you want to send it to server during normal html page submit, put the return value in hidden field.

如果要在正常的html页面提交期间将其发送到服务器,请将返回值放在隐藏字段中。

If you want to send the value before the form submit, use AJAX.

如果要在表单提交之前发送值,请使用AJAX。

Hidden Field method

隐藏场方法

JavaScript

function test(abc) {
         var ddlArray = new Array();
            var ddl = document.getElementById('AdjusterList');
          for (i = 0; i < ddl.options.length; i++) {
            ddlArray[i] = ddl.options[i].value;
          }
  var indexsel = ddl.selectedIndex;
      document.getElementById("returnValueField").value = indexsel;
      return indexsel ;            
     }

HTML

<input type="hidden" id="returnValueField" name="returnValueField" />

In ASP access this hidden field like an other form field.

在ASP中访问此隐藏字段,就像其他表单字段一样。

For AJAX use some library like jQuery to make things easier.

对于AJAX,使用像jQuery这样的库来简化操作。

Using AJAX with jQuery

将AJAX与jQuery一起使用

First you need to include the jQuery library in your page.

首先,您需要在页面中包含jQuery库。

Then modify your function like this

然后像这样修改你的功能

function test(abc) {
             var ddlArray = new Array();
                var ddl = document.getElementById('AdjusterList');
              for (i = 0; i < ddl.options.length; i++) {
                ddlArray[i] = ddl.options[i].value;
              }
      var indexsel = ddl.selectedIndex;
          // Ajax call starts
          $.ajax({
                 url: "your_asp_page_to_handle_request.asp",
                 data: {"selected_index": indexsel },
                 success: function() { 
                    alert("I am back after sending data sucessfully to server.");}
                 });
          // Ajax call ends  

          return indexsel ;            
         }

Your ASP code in your_asp_page_to_handle_request.asp will be something like this

your_asp_page_to_handle_request.asp中的ASP代码将是这样的

<%

dim selectedIndex
selectedIndex = Request.QueryString("selected_index")

%>

Please note that you can also use jQuery.get() instead of the Ajax function we used above.

请注意,您也可以使用jQuery.get()代替我们上面使用的Ajax函数。

#1


5  

If you want to send it to server during normal html page submit, put the return value in hidden field.

如果要在正常的html页面提交期间将其发送到服务器,请将返回值放在隐藏字段中。

If you want to send the value before the form submit, use AJAX.

如果要在表单提交之前发送值,请使用AJAX。

Hidden Field method

隐藏场方法

JavaScript

function test(abc) {
         var ddlArray = new Array();
            var ddl = document.getElementById('AdjusterList');
          for (i = 0; i < ddl.options.length; i++) {
            ddlArray[i] = ddl.options[i].value;
          }
  var indexsel = ddl.selectedIndex;
      document.getElementById("returnValueField").value = indexsel;
      return indexsel ;            
     }

HTML

<input type="hidden" id="returnValueField" name="returnValueField" />

In ASP access this hidden field like an other form field.

在ASP中访问此隐藏字段,就像其他表单字段一样。

For AJAX use some library like jQuery to make things easier.

对于AJAX,使用像jQuery这样的库来简化操作。

Using AJAX with jQuery

将AJAX与jQuery一起使用

First you need to include the jQuery library in your page.

首先,您需要在页面中包含jQuery库。

Then modify your function like this

然后像这样修改你的功能

function test(abc) {
             var ddlArray = new Array();
                var ddl = document.getElementById('AdjusterList');
              for (i = 0; i < ddl.options.length; i++) {
                ddlArray[i] = ddl.options[i].value;
              }
      var indexsel = ddl.selectedIndex;
          // Ajax call starts
          $.ajax({
                 url: "your_asp_page_to_handle_request.asp",
                 data: {"selected_index": indexsel },
                 success: function() { 
                    alert("I am back after sending data sucessfully to server.");}
                 });
          // Ajax call ends  

          return indexsel ;            
         }

Your ASP code in your_asp_page_to_handle_request.asp will be something like this

your_asp_page_to_handle_request.asp中的ASP代码将是这样的

<%

dim selectedIndex
selectedIndex = Request.QueryString("selected_index")

%>

Please note that you can also use jQuery.get() instead of the Ajax function we used above.

请注意,您也可以使用jQuery.get()代替我们上面使用的Ajax函数。