【问题标题】:JQuery .on event not firingJQuery .on 事件未触发
【发布时间】:2014-12-19 23:03:02
【问题描述】:

我有一个关于事件的 jquery,以便将一些数据传递到引导模式窗口。这是模态的代码:

<div class="modal fade" id="renameFile" tabindex="-1" role="dialog" aria-labelledby="renameFile" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">close</span></button>
            <h4 class="modal-title" id="formRenameTitle">rename</h4>
        </div>
        <div class="modal-body">
            <form method="post" th:action="files">
                <div class="input-group">
                    <input type="text" class="form-control" id="fileName" name="fileName" />
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit">rename</button>
                    </span>
                </div>
                <input type="hidden" name="action" value="EDIT" />
                <input type="hidden" name="fileId" />
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
        </div>
    </div>
</div>
</div>

这是按钮的代码(它通常位于网格的单元格中):

<a data-toggle="modal" data-id="${{file.id}}" data-name="${{file.name}}" title="rename" class="open-renameFile" href="#renameFile">
    <span class="glyphicon glyphicon-pencil" aria-hidden="true</span>rename
</a>

我正在使用 thymeleaf 填充每一行的单元格。

我在页面底部添加了这个 javascript:

<script type="text/javascript">
$(document).on("click", ".open-renameFile", function () {
    var fileName = $(this).data('name');
    $(".modal-body #fileName").val( fileName );
});
$(document).on("click", ".open-renameFile", function () {
    var fileId = $(this).data('id');
    $(".modal-body #fileId").val( fileId );
});     
</script>

【问题讨论】:

  • 你怎么知道事件没有被触发?您确定您的 ids 在文档中是唯一的吗?我的意思是像.modal-body #fileXXXX 这样的选择器没有意义。只需使用#fileXXXX
  • 我在脚本中抛出了一个警报,但它从未弹出。此外,该字段未填充。

标签: javascript jquery twitter-bootstrap events thymeleaf


【解决方案1】:

不确定您的代码的意义是什么,因为您实际上并没有对 val 做任何事情?但是试试这个:

<script type="text/javascript">
$('.open-renameFile').click(function () {
    console.log('Event fired!');
    var fileName = $(this).data('name');
    $(".modal-body #fileName").val( fileName );
    var fileId = $(this).data('id');
    $(".modal-body #fileId").val( fileId );
});     
</script>

【讨论】:

  • 还是不行。另外,我在我的帖子中使用这些值,我需要它们在服务器端进行数据库更新。
  • 字符串是否打印到控制台?
  • 不,控制台没有打印任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多