javascript - MVC 3 中带有 jQ​​uery 的低调 JavaScript

标签 javascript jquery asp.net-mvc asp.net-mvc-3

假设在我拥有的 MVC 3 应用程序中的某处

if (Model.ImageListGallery != null)
{
    <h3>@ImagesTranslation.Gallery</h3>

    foreach (var imageInGallery in Model.ImageListGallery)
    {
        <div id="@imageInGallery.IdImage">
            <a rel="group" href= "@Url.Action("displaybig", "news", new { idNews = Model.IdNews, idImage = imageInGallery.IdImage })">
                <img src= "@Url.Action("displaysmall", "news", new { idNews = Model.IdNews, idImage = imageInGallery.IdImage })" alt=""/></a>
            @Html.Label(ImagesTranslation.Description)
            @Html.TextArea("Description", imageInGallery.Description, new { id = "area" + imageInGallery.IdImage, onfocus = "removeDisabledBtnOnImage('" + imageInGallery.IdImage + "')" })
            <button disabled="disabled" id="btn@(imageInGallery.IdImage)" onclick="saveDescription('@imageInGallery.IdImage')">@CommonTranslations.Save</button>
            <img class="@imageInGallery.IdImage" src="@Href("~/Content/delete.png")" onclick="deleteImage('@imageInGallery.IdImage')" title="@ImagesTranslation.DeleteImage" alt=""/>
        </div>
    }
}

在那种情况下(事件 onfocus 和 onclick?),您将如何使用不引人注目的 JavaScript 和 jQuery?

最佳答案

通过向父 div 添加一个类,您可以将每个画廊图像识别为需要将处理程序添加到其元素的东西:

foreach (var imageInGallery in Model.ImageListGallery)
{
    <div id="@imageInGallery.IdImage" class="gallery-image">
        <a rel="group" href= "@Url.Action(...)">
            <img src= "@Url.Action(..." alt=""/></a>
        @Html.Label(ImagesTranslation.Description)
        @Html.TextArea("Description", imageInGallery.Description, new { id = "area" + imageInGallery.IdImage })
        <button disabled="disabled" id="btn@(imageInGallery.IdImage)">@CommonTranslations.Save</button>
        <img class="@imageInGallery.IdImage" src="@Href("~/Content/delete.png")" title="@ImagesTranslation.DeleteImage" alt=""/>
    </div>
}
$(function() {
    $('.gallery-image').each(function() {
        var t = $(this);
        var imageId = t.attr('id');
        t.find('textarea').onfocus(function() {
            removeDisabledBtnOnImage(imageId);
        });
        t.find('btn').onclick(function(){
            saveDescription(imageId);
        });
        t.find('img').onclick(function(){
            deleteImage(imageId);
        });
    });
});

还有其他方法可以解决这个问题,但希望这能让您大致了解。

关于javascript - MVC 3 中带有 jQ​​uery 的低调 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8183776/

相关文章:

c# - 找不到 Asp.net MVC 4 Url 资源

asp.net-mvc - Identity 2.0 使用 token 重置短信密码

javascript - 将函数中的多个值添加到每个 jQuery

php - 如何从表格内容中获取记录作为弹出表单的数据并显示

javascript - 即使我使用以太坊官方网页中的示例也无法发送交易

在队列中延迟的 Javascript 事件执行

javascript - Highcharts 直线值

javascript - 当元素位于窗口顶部时添加类

php - 移动产品描述 Div Woocommerce

c# - DropDownList Bootstrap 样式