Asp.net mvc colorbox内的部分页面无法触发单选按钮的onclick处理程序

Asp.net mvc colorbox内的部分页面无法触发单选按钮的onclick处理程序,asp.net-mvc,Asp.net Mvc,在我的索引页中,我有 <a class='popup' href='@Url.Action("Add", "Alert")'>Add New</a> 我在cshtml文件的末尾有一个节脚本 @section Scripts { <link rel="stylesheet" href="http://www.formmail-maker.com/var/demo/jquery-popup- form/colorbox.css" /> <s

在我的索引页中,我有

<a class='popup' href='@Url.Action("Add", "Alert")'>Add New</a>

我在cshtml文件的末尾有一个节脚本

@section Scripts 
{ 
<link rel="stylesheet" href="http://www.formmail-maker.com/var/demo/jquery-popup-    form/colorbox.css" />

<style>
    #cboxOverlay{ background:#666666; }
</style>

<script type="text/javascript" src="http://www.jacklmoore.com/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $(".popup").colorbox({ opacity: 0.85, iframe: true, fastIframe: false, width:    "450px", height: "480px", scrolling: false });
});

function ddUserClicked() { 
{
    alert('user button, selected!'); 
}

function ddEntityClicked() {
    alert('entity button, selected!');
}

</script>

}
@节脚本
{ 
#cboxOverlay{背景:#666666;}
$(文档).ready(函数(){
$(“.popup”).colorbox({opacity:0.85,iframe:true,fastIframe:false,宽度:“450px”,高度:“480px”,滚动:false});
});
函数ddUserClicked(){
{
警报('用户按钮,已选择!');
}
函数ddEntityClicked(){
警报('实体按钮,选中!');
}
}
当调用Add(action-method)警报(controller)时,它会返回一个部分页面,该页面会充满页面中心的颜色框

在颜色框中,我有两个单选按钮。我希望使用onclick事件,以便它们可以调用我从原始视图ddUserClicked()和ddEntityClicked()实现的函数

但这是行不通的

部分页面脚本是

@model WellRoute.Web.Models.CreateAlertModel

@{
    ViewBag.Title = "Add New Alert";
}

<h2>@ViewBag.Title</h2><br />

@using (Html.BeginForm("Add", "Alert", FormMethod.Post))
{
<fieldset>
    <legend>New Alert</legend>
    <table>
        <tr>
            <td>@Html.Label("Type")</td>
            <td>
                @Html.RadioButtonFor(m => m.IsUserAlert, true, new { onclick = "ddUserClicked(); ", })User Alert
                @Html.RadioButtonFor(m => m.IsUserAlert, false, new { onclick = "ddEntityClicked()", })Entity Alert
            </td>
        </tr>
        <tr>
            <td>@Html.Label("User")</td>
            <td>
                @Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.Users, "Id", "EmailAddress"), new { style = "width:250px;" })
                @Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.Entities, "Id", "Name"), new { style = "width:250px; visibility:hidden;"     })
            </td>
        </tr>
        <tr>
            <td>@Html.Label("Message")</td>
            <td>@Html.TextAreaFor(m => m.Message, new  { style = "width:250px; height:100px" })</td>
        </tr>
    </table>
    <p>
        <input type="submit" value="Save" />
        <input type="submit" value="Cancel" />
    </p>
</fieldset>
}
@model WellRoute.Web.Models.CreateAlertModel
@{
ViewBag.Title=“添加新警报”;
}
@ViewBag.Title
@使用(Html.BeginForm(“Add”、“Alert”、FormMethod.Post)) { 新警报 @Html.Label(“类型”) @RadioButton(m=>m.IsUserAlert,true,新建{onclick=“ddUserClicked();”,})用户警报 @RadioButton(m=>m.IsUserAlert,false,新的{onclick=“ddEntityClicked()”,})实体警报 @Html.Label(“用户”) @DropDownListFor(m=>m.SelectedValue,新的SelectList(Model.Users,“Id”,“EmailAddress”),新的{style=“width:250px;”) @DropDownListFor(m=>m.SelectedValue,new SelectList(Model.Entities,“Id”,“Name”),new{style=“width:250px;可见性:hidden;”}) @Html.Label(“消息”) @text区域(m=>m.Message,新的{style=“宽度:250px;高度:100px”})

}
我看到的第一件事(不确定这是否只是一个复制粘贴错误)是您的
{
ddUserClicked()上有一个
太多了。
-function:)

除此之外,一种快速而肮脏的方法是从视图中删除函数
ddUserClicked
ddEntityClicked
,并将以下内容放在局部视图的顶部(但不在
脚本
部分中)

…然后创建一个jquery脚本。大致如下:

// File alert.js
$(document).ready(function() {
    $("#user_alert").click(function() {
        alert('user button, selected!');
    });

    $("#entity_alert").click(function () {
        alert('user button, selected!');
    });
});

然后链接.js。这将使您与脚本松散耦合,因为您没有在HTML中指定任何要绑定的函数。通常,您希望避免在视图中添加太多javascript。

我刚刚意识到,我不确定click()是否可以与单选按钮一起使用。我自己还没有尝试过。可能是change()你在找我。
<td>
    @Html.RadioButtonFor(m => m.IsUserAlert, true, new { id = "user_alert", })User Alert
    @Html.RadioButtonFor(m => m.IsUserAlert, false, new { id = "entity_alert", })Entity Alert
</td>
// File alert.js
$(document).ready(function() {
    $("#user_alert").click(function() {
        alert('user button, selected!');
    });

    $("#entity_alert").click(function () {
        alert('user button, selected!');
    });
});