我有一个下拉列表和 Onchange,我想向我的 Controller 发送 Ajax 请求,如何获取实际数据或所选下拉列表选项的值以放入我的 Ajax 回调中。
$('#TypeFeed').change(function () {
$.ajax({
url: "Account/changefeed",
type: "GET",
// how can I get selected value here
});
});
// This is my dropdownList below id=TypeFeed
@Html.DropDownList("TypeFeed", new SelectList(new List<Object>{
new { Text = "Local Issues", Value = "Local Issues"},new { Text ="sports", Value= "sports"}}, "Value", "Text"))
我想在此处发送值,但必须首先在回调中捕获它
public PartialViewResult changefeed(string TypeFeed)
最佳答案
使用val()
方法获取所选选项的值。
$(function(){
$('#TypeFeed').change(function () {
var typeFeed=$(this).val();
//do ajax now
$.get("@Url.Action("changefeed","Home")?TypeFeed="+typeFeed,function(res){
//do something with res now
});
});
});
考虑在发送之前对值进行编码。您可以使用encodeURIComponent
方法。
您也可以通过这种方式发送所选项目的值
$.get("@Url.Action("changefeed","Home")",{ TypeFeed:typeFeed},function(res){
//do something with res now
});
关于jquery Ajax 下拉列表中的 onchange 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20225823/