0

我在特定视图中添加了 MvcContrib 网格。在网格中,除了列数据,我还有一个单选按钮列。我正在尝试获取选定的单选按钮单击事件,但它没有按预期发生。我不知道实施有什么问题。你们可以看看并给出一些建议或代码片段来解决这个问题。

    <%= Html.Grid(Model.Items)
        .Columns(column =>
                     {
                         column.For(x => x.Name)
                             .Named(Html.OrderBy(null, "DisplayRestaurantsList", "Group", ViewContext.RouteData.Values, "sortBy", "sortDir", true, Model.SortDirection, Model.SortBy, "restaurantname", "Restaurant"));
                         column.For(x => x.Contact.GetDisplayName())
                             .Named(Html.OrderBy(null, "DisplayRestaurantsList", "Restaurant", ViewContext.RouteData.Values, "sortBy", "sortDir", true, Model.SortDirection, Model.SortBy, "administrator", "Administrator"));
                         column.For(x => Html.ActionLink("Remove", "RemoveRestaurant", "Group", new { id = x.ID, GroupID=x.Group.ID }, null))
                            .DoNotEncode();
                         column.For(x => Html.RadioButton("defaultRastaurant",x.ID,(!x.Group.ID.Value.ToString().IsEmptyOrNull() && x.ID==x.Group.DefaultRestaurantID) ? true:false)).Named("default").DoNotEncode();
                     })
        .Attributes(@class => "table-1 gapmb40")
        .Empty("There are no restaurants that match your criteria.")
        .RowStart(row => string.Format("<tr{0}>", row.IsAlternate ? "style=\"background-color:#CCDDCC\"" : ""))

  %>

jQuery代码选择单选按钮点击:

$("input[type='radio']").click(function () {
               alert("kaustubh");
           });

也尝试过以下一个:

 $("input[name='defaultRastaurant']").change(function () {
           alert("kaustubh");               
                          });
4

1 回答 1

1

Looks like its a event delegation issue. The element is not present in the DOM when the event handler is associated to it..

Try this

$("body").on("click"," input[type='radio']" ,function () {
       alert("kaustubh");
 }); 

The body can be replaced by any non static parent in which the radiobuttons are present..

于 2012-10-16T05:46:28.343 回答