1

我浏览了几篇文章和几个代码,但它们似乎都不起作用。

我需要根据同一页面上的 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>

和视图模型:

public class ModelViewModel
{
    public int idSelected { get; set; }

    [Display(Name = "Usuários Cadastrados")]
    public IEnumerable<SelectListItem> Users { get; set; }
}

但我仍然有一个问题:如何将选定的字段传递给控制器​​操作?我尝试使用 DropDownListFor,但在这种情况下,我丢失了对象名称,并且 Jscript 不起作用。

4

2 回答 2

3

尝试这个:

$(document).ready(function () {
    $('select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]').live('change',function () {
        $(this).closest('form').submit();
    });
});

从作为元字符的选择器中删除列“:”并使选择器无效,您也不需要它们。

要么尝试使用 id 或获取表单的所有输入字段,使用jquery :input 选择器 $('form :input')来选择表单的所有输入字段。

IE

$('form').find(':input').live('change',function () {
     $(this).closest('form').submit(); 
});

另请注意 live 已被弃用,如果您使用 jquery version >=1.7 使用 on() 而不是 live

于 2013-09-18T02:19:33.040 回答
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();
    });
});
于 2013-12-05T00:30:20.220 回答