我浏览了几个帖子和几个代码,但似乎都没有。
我需要在同一页面,部分视图上基于DropDown选定键重新加载WebGrid。我现在正在构建下拉元素并在视图中使用此代码:
@Html.DropDownListFor(model => model.Users, Model.Users,
new { autopostback = "true" })
以下javascript代码:
<script type="text/javascript">
$(document).ready(function () {
$('select:[autopostback=true],input[type=checkbox]:[autopostback=true],input[type=radio]:[autopostback=true]')
.live('change',function () {
$(this).closest('form').submit();
});
});
</script>
浏览器的Javascript控制台说:
Uncaught Error:Syntax error, unrecognized expression:select:[autopostback=true],
input[type=checkbox]:[autopostback=true],
input[type=radio]:[autopostback=true]
Controller没有打电话。我究竟做错了什么?
感谢。
[编辑]
由于现在正在运作,到目前为止正在进行的工作:
<script type="text/javascript">
$(function () {
$("#UserList").change(function (e) {
var _this = $(this);
$.post("@Url.Action("List","MainController", Model)", _this.closest("form").serialize(),
function (response) {
// do something with response.
});
});
观点:
<td class="tdatadata">@Html.DropDownList("UserList", Model.Users, new { autopostback = "true" })</td>
ViewModel:
public class ModelViewModel
{
public int idSelected { get; set; }
[Display(Name = "Usuários Cadastrados")]
public IEnumerable<SelectListItem> Users { get; set; }
}
但我仍有问题:如何将所选字段传递给控制器操作?我尝试使用DropDownListFor,但在这种情况下,我松开了对象名称,而Jscript不起作用。
答案 0 :(得分:3)
试试这个:
$(document).ready(function () {
$('select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]').live('change',function () {
$(this).closest('form').submit();
});
});
从选择器中删除列“:”,这是一个元字符并使选择器无效,你也不需要它们。
EIther尝试使用id或获取表单的所有输入字段使用jquery :input selector $('form :input')
来选择表单的所有输入字段。
即
$('form').find(':input').live('change',function () {
$(this).closest('form').submit();
});
另请注意,live已被弃用,如果您使用的是jquery版本&gt; = 1.7则使用on()而不是live
答案 1 :(得分:2)
尝试删除:
并按.live
更改.on
。
$(document).ready(function () {
$('select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]').on('change',function () {
$(this).closest('form').submit();
});
});