I have a drop down list
我有一个下拉列表
<select onchange="alert(this.value);">
<option selected="selected" value="cat">cat</option>
<option value="dog">dog</option>
</select>
I would like to make it so that when the users changes a value then an AJAX call is sent to my MVC controller which then updates the database.
我想这样做,以便当用户更改值时,然后将AJAX调用发送到我的MVC控制器,然后更新数据库。
I've done this with forms but never from javascript. Does anyone have an example of how this could be done.
我用表单完成了这个,但从来没有从javascript。有没有人有一个如何做到这一点的例子。
thanks,
谢谢,
1 个解决方案
#1
1
If you are using jQuery:
如果你使用jQuery:
<select id="category" name="category">
<option selected="selected" value="cat">cat</option>
<option value="dog">dog</option>
</select>
and then:
接着:
$(function() {
$('#category').change(function() {
$.post('/home/save', { selectedCategory: $(this).val() }, function(result) {
alert('success');
});
});
});
which will send an AJAX request to the following action:
这会将AJAX请求发送到以下操作:
[HttpPost]
public ActionResult Save(string selectedCategory)
{
// TODO: process the selected category
return Json(new { success = true });
}
#1
1
If you are using jQuery:
如果你使用jQuery:
<select id="category" name="category">
<option selected="selected" value="cat">cat</option>
<option value="dog">dog</option>
</select>
and then:
接着:
$(function() {
$('#category').change(function() {
$.post('/home/save', { selectedCategory: $(this).val() }, function(result) {
alert('success');
});
});
});
which will send an AJAX request to the following action:
这会将AJAX请求发送到以下操作:
[HttpPost]
public ActionResult Save(string selectedCategory)
{
// TODO: process the selected category
return Json(new { success = true });
}