在 Twitter 引导模式中输入按键时触发 onClick 事件
Trigger onClick event when Enter keypress in a Twitter Bootstrap Modal
我在 Twitter Bootstrap Modal 之上构建了一些模态,每个模态至少有一个提交按钮,这是在大多数情况下我需要触发的默认操作,因为第二个按钮用于返回(关闭模态),在模态上,当我按回车键时,我可以触发提交的任何操作?例如现在,因为它们是按钮,我有这样的东西:
$("#btn").on("click", function(e){
e.preventDefault(); // wait... not send form yet, will be a Ajax call
});
有什么帮助吗?示例代码?
作为参考,这是模态内容(我与Twig一起使用作为Symfony2项目的一部分,所以不用担心{{ }}
):
<div class="modal fade " id="addNorma" tabindex="-1" role="dialog" aria-labelledby="addNorma" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">{{ 'boton.cerrar'|trans({}, 'AppBundle') }}</span></button>
<h4 class="modal-title" id="myModalLabel">{{ 'modal.normas.titulo'|trans({}, 'AppBundle') }}</h4>
</div>
<div class="modal-body">
<form id="buscadorNorma" method="POST">
<div class="spacer10"></div>
<div class="row">
<div class="col-md-5 col-lg-5 col-sm-5 col-xs-5">
<label for="codigo_norma">{{ 'modal.normas.codigo'|trans({}, 'AppBundle') }}</label>
<input type="text" class="form-control" id="codigo_norma" name="codigo_norma" placeholder="{{ 'modal.normas.codigoPlaceholder'|trans({}, 'AppBundle') }}">
</div>
<div class="col-md-5 col-lg-5 col-sm-5 col-xs-5 col-md-offset-2">
<label for="ano_publicacion">{{ 'modal.normas.anno'|trans({}, 'AppBundle') }}</label>
<input type="text" class="form-control" id="ano_publicacion" name="ano_publicacion" placeholder="{{ 'modal.normas.annoPlaceholder'|trans({}, 'AppBundle') }}">
</div>
</div>
<div class="spacer10"></div>
<div class="row">
<div class="col-md-12">
<label for="nombre_norma">{{ 'modal.normas.term'|trans({}, 'AppBundle') }}</label>
<input type="text" class="form-control" id="nombre_norma" name="nombre_norma" placeholder="{{ 'modal.normas.termPlaceholder'|trans({}, 'AppBundle') }}">
</div>
</div>
<div class="spacer10"></div>
<div class="row">
<div class="col-md-12">
<label for="procedencia_producto">{{ 'modal.normas.comite'|trans({}, 'AppBundle') }}</label>
<div class="form-group">
<div>
<select name="comite_tecnico" id="comite_tecnico" class="form-control toSelect2">
<option value="" selected="selected">{{ 'seleccioneOpcion'|trans({}, 'AppBundle') }}</option>
{% for key, item in comiteTecnico %}
<option value="{{ key }}">{{ item.nombre }}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</div>
<div class="spacer5"></div>
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-default pull-right" disabled="disabled" id="btnBuscarNorma"><i class="fa fa-binoculars"></i> {{ 'boton.buscar'|trans({}, 'AppBundle') }}</button>
</div>
</div>
</form>
<div class="spacer10"></div>
<table class="table table-hover table-condensed" id="resultadoNorma" style="display: none">
<thead>
<tr>
<th><input type="checkbox" id="toggleCheckboxNorma" name="toggleCheckboxNorma" /></th>
<th>{{ 'columnas.normas.no'|trans({}, 'AppBundle') }}</th>
<th>{{ 'columnas.normas.norma'|trans({}, 'AppBundle') }}</th>
<th>{{ 'columnas.normas.anno'|trans({}, 'AppBundle') }}</th>
<th>{{ 'columnas.normas.comite'|trans({}, 'AppBundle') }}</th>
</tr>
</thead>
<tbody id="resultadoNormaBody"></tbody>
</table>
<div class="alert alert-danger" role="alert" style="display: none;" id="sinResultadosBuscarNormas">
{{ 'mensajes.msgNoEncontrado'|trans({}, 'AppBundle') }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">{{ 'boton.regresar'|trans({}, 'AppBundle') }}</button>
<button type="submit" class="btn btn-primary" id="btnAplicarNorma" disabled="disabled"><i class="fa fa-save"></i> {{ 'boton.aplicar'|trans({}, 'AppBundle') }}</button>
</div>
</div>
</div>
</div>
这就是我单击#btnBuscarNorma
时触发的代码:
$('button#btnBuscarNorma').on('click', function (ev) {
ev.preventDefault();
$.post(Routing.generate('filtrarNormas'), $('#buscadorNorma').serialize(), 'json')
.done(function (data, textStatus, jqXHR) {
$('#resultadoNorma').toggle(!!data.entities.length);
$("#sinResultadosBuscarNormas").toggle(!data.entities.length);
if (data.entities.length > 0) {
var $html = '';
data.entities.forEach(function (value, index, array) {
$html += '<tr>';
$html += '<td><input type="checkbox" id="' + value.id + '" value="' + value.id + '"></td>';
$html += '<td>' + value.codigo + '</td>';
$html += '<td>' + value.norma + '</td>';
$html += '<td>' + value.anno + '</td>';
$html += '<td>' + value.comiteTecnico + '</td>';
$html += '</tr>';
});
$("table tbody#resultadoNormaBody").html($html);
marcarTodosCheck('#toggleCheckboxNorma', '#resultadoNormaBody');
}
});
});
这个想法是Enter
键将触发#btnBuscarNorma
点击事件,而不是#btnAplicarNorma
点击事件。
打开模态时绑定此事件。
//to prevent multiple binding
$(document).unbind("keyup").keyup(function(e){
var code = e.which; // recommended to use e.which, it's normalized across browsers
if(code==13)
{
$("#btn").click();
}
});
您可以监听表单提交并阻止它。
$('#myform').on('submit', function (e) {
e.preventDefault();
});
通过这种方式,您可以确保永远不会提交表单。您还可以在调用提交函数时启动 Ajax 调用。
普通按钮<button>
也不会提交表单。
当在类中打开模态时,它处于活动状态。所以你可以进入JQuery:
$("#addNorma.in").on("keydown",function(e){
if(e.which == 13){
//Do something.
}
});
相关文章:
- 如何在执行此特定onclick事件时执行JavaScript函数
- 如何调用“;链接_;在onclick事件上使用Javascript
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 选择onclick事件jquery
- 如何刷新列表框内容onclick或blur事件
- 执行php函数的onclick事件的其他替代方案
- 动态创建OnClick事件Javascript
- onClick事件未触发reactjs
- 使用java脚本更改onClick事件中菜单选项卡的颜色
- 如何使用OnClick事件通过参数调用来调用PHP函数
- 第二个事件[onClick]不起作用
- 复选框事件onclick不起作用
- 如何在jQuery中为通知添加事件onClick
- JS函数不响应事件onclick
- 使用事件onclick访问if语句Javascript中变量的值
- 如何在事件onclick中抓取文本
- 如何绑定id < light >与事件Onclick
- 未找到JS事件OnClick
- 如何在js/prototype中触发具有特定顺序的事件(onclick)
- 无法触发javascript事件OnClick的复选框