【问题标题】:Add Button to parent div statically将按钮静态添加到父 div
【发布时间】:2016-08-24 16:33:03
【问题描述】:

定义:我正在创建一个管理实习生的项目。目前我在员工方面工作;员工可以添加/编辑/删除实习生。这些动作由 modal popups 处理。

方法:为了避免不必要的代码,我创建了一个布局模式。

<div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3><span class="glyphicon glyphicon-pencil"></span>Intern</h3>
            </div>

            <div id="myModalContent"></div>

            <div class="modal-footer">
                <button type="submit" class="btn btn-default btn-default pull-right" data-dismiss="modal">&nbsp;Cancel
                </button>

            </div>
        </div>

如您所见,布局有 CANCEL 按钮。因为每个模态都应该有取消按钮,所以最好的方法是把它放到布局中。

<div id="myModalContent"></div>

这个 myModalContent 部分由 javascript/ajax 代码填充。脚本将 部分视图 放入 myModalContent。 “保存更改”、“删除实习生”等按钮也来自部分视图。

问题: 但是 myModalContent 在另一个 div 中,取消按钮在另一个 div 中。这导致了一个问题:

编辑按钮代码:

<div class="modal-footer">
        <button type="submit" class="btn btn-default btn-default pull-right">
            <span class="glyphicon glyphicon-floppy-disk"></span> Edit Intern
        </button>
    </div>

我希望这些按钮位于同一行。据我所知(根据我的研究),我无法使用 css/html 访问父 div。

任何帮助将不胜感激。谢谢

编辑:

jquery代码在这里:

$(function () {
$.ajaxSetup({ cache: false });
$("a[data-modal]").on("click", function (e) {
    $('#myModalContent').load(this.href, function () {
        $('#myModal').modal({
            keyboard: true
        }, 'show');
        bindForm(this);
    });
    return false;
});



function bindForm(dialog) {
$('form', dialog).submit(function () {
    $('#progress').show();
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function (result) {
            if (result.success) {
                $('#myModal').modal('hide');
                $('#progress').hide();
                location.reload();
            } else {
                $('#progress').hide();
                $('#myModalContent').html(result);
                bindForm();
                location.reload();
            }
        }
    });
    return false;
});

【问题讨论】:

  • 有什么理由不使用 jQueryUI?然后你可以在js中定义按钮。
  • 那是因为我不知道 jQueryUI(我刚刚听说过,哈哈)。这是我的实习项目,我只使用了一周的 jquery、javascript、html、css。 AFAIK 这不是一个选择。还是谢谢。
  • 在我看来,您只是在此模式中添加部分代码。很明显,编辑和取消按钮已关闭,因为您有这样的结构
    所以这就是它看起来像的原因

标签: javascript jquery html css asp.net-mvc


【解决方案1】:

您可以从myModalContent 获取编辑按钮,并在使用下面的 jquery 加载部分视图后将其附加到modal-footer

else {
       $('#progress').hide();
       $('#myModalContent').html(result);
       //move button from modal content to footer
       $('div.modal-footer').append($('#myModalContent button.btn.btn-default.pull-right'));
       bindForm();
       location.reload();
   }

如果需要,请在取消按钮之前编辑按钮,然后使用 prepend() 而不是 append()

【讨论】:

  • 我明白了,将按钮附加到 div。但是因为我(我是新手)它不起作用,我应该把这个 jquery 放在哪里?
  • 就在您放置部分视图的 jquery 代码下方。请发布您的jquery代码,我会帮助您
  • 我已经更新了答案,你需要在$('#myModalContent').html(result);之后添加jquery代码
  • 这暂时没有任何改变。
  • 您是否使用第一个 ajax 调用来加载模式?然后将我的 jquery 代码放在bindform 调用之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多