[code page="c#"]
[WebMethod]
public static int bubblesort(int[] arr)
{
//some code here
return arr;
}
var text;
$(document).ready(function() {
$("#submit").on("click", function() {
text = $("#text1").val();
option = $('#dropdownfilter').val();
bsortedarr(text);
});
});
function bsortedarr(text) {
$.ajax({
type: "POST",
url: "default.aspx/bubblesort",
data: JSON.stringify({ arr: text }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
//code
},
error: function(response) {
//code
}
});
}
我创建了 WebMethod,它使用冒泡排序返回排序后的数组列表。当我选择选项 bsort 并将整数作为输入(例如 100,10,1)输入文本框时。因此,我使用 stringify({ arr: text})
将该文本框值传递给 ajax 调用。我想将此文本值“100,10,1”传递给 WebMethod,但它没有发生。所以我试图用','拆分它并想返回数组格式列表但它是整数类型所以无法返回。
最佳答案
您应该首先定义atxt
变量。它将保存文本框的值,代码如下所示:
var atxt = $('TEXTBOX_SELECTOR').val();
然后使用这个变量通过ajax传递值。
关于javascript - 如何使用 ajax jQuery 将文本框的整数值发送到 [WebMethod],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020676/