javascript 如何将客户端javascript返回值传递给ASP经典服务器端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11446502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass client side javascript return value to ASP classic server side
提问by user1093452
javascript
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
ASP
strArrayCRN = osRecordSet.RecordCount
strArrayCRN2 = osRecordSet2.RecordCount
dim StrCount
StrCount =strArrayCRN+strArrayCRN2
HTML +ASP
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 服务器端
回答by kiranvj
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
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
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 jQueryto make things easier.
对于 AJAX,使用一些像jQuery这样的库来使事情变得更容易。
Using AJAX with jQuery
在 jQuery 中使用 AJAX
First you need to includethe 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.aspwill 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 函数。