无法为(稍后的)javascript生成的元素传播事件侦听器(提交表单)
Not able to propagate event listener (submit for form) for element generated by(later) javascript
我有像这样的html代码 -
<div class = "setup_eda_class" id = "Setup_eda">
<p>
<a> <span id="calendar_eda_form_top_x"> x </span> </a>
<br> </br>
<div id="insert_calendar_eda_form"> </div>
</p>
</div>
最初插入div "insert_calendar_eda_form"。
脚本代码
calendar_eda_add_form = function (clicked_id, arraylength){
htmlstr = "";
htmlstr += '<a> Add Events for - </a>' ;
htmlstr += '<form id="calendar_eda_add_form" action="/setupaddcalendarevent" method="POST">'
htmlstr += '<label for="start_date">Start Date</label>'
htmlstr += '<input type="text" name="start_date" />'
htmlstr += '<label for="end_date">End Date</label>'
htmlstr += '<input type="text" name="end_date" />'
htmlstr += '<br> </br>'
htmlstr += '<input type="submit" value="Add Event" />'
htmlstr += '</form>'
$("#insert_calendar_eda_form").html(htmlstr);
"calendar_eda_add_form"html 形式由 Javascript 动态创建。
事件侦听器的 HTML 代码
<script type="text/javascript">
$(document).ready(function(){
$('#insert_calendar_eda_form').on('submit', 'calendar_eda_add_form', function(event){
alert ('clicked add event');
event.preventDefault();
somefunction();
});
});
Calendar_eda_add_form由 JS 动态生成。我正在尝试传播"提交"事件,但似乎不起作用。你能帮忙吗?
缺少 .on() 内选择器上的哈希标签
$(document).ready(function(){
$('#insert_calendar_eda_form').on('submit', '#calendar_eda_add_form', function(event){
alert ('clicked add event');
event.preventDefault();
somefunction();
});
});
这是一个小提琴,显示它 http://jsfiddle.net/ja3kaa4b/工作 - 您是否在浏览器中看到任何控制台错误?
您可能遇到重复 ID 的影响。在函数中calendar_eda_add_form
将窗体的 ID 更改为类,并改用以下代码:
$(document).ready(function(){
$('#insert_calendar_eda_form').on('submit', '.calendar_eda_add_form', function(event){
alert ('clicked add event');
event.preventDefault();
somefunction();
});
});
更新
根据问题下的评论,上述情况不太可能,因为表单仅添加到 DOM 一次。但是,正在使用的 HTML 无效:
<div class = "setup_eda_class" id = "Setup_eda">
<p>
<a> <span id="calendar_eda_form_top_x"> x </span> </a>
<br> </br>
<div id="insert_calendar_eda_form"> </div>
</p>
</div>
根据 HTML 规范,P 元素只能包含内联元素;div - 目标div - 不是内联元素。
- 为什么
标签不能包含
标签? - http://www.w3.org/TR/html4/sgml/dtd.html
如果我理解你的问题,这应该有效:
calendar_eda_add_form = function (clicked_id, arraylength){
htmlstr = "";
htmlstr += '<a> Add Events for - </a>' ;
htmlstr += '<form id="calendar_eda_add_form" action="/setupaddcalendarevent" method="POST">'
htmlstr += '<label for="start_date">Start Date</label>'
htmlstr += '<input type="text" name="start_date" />'
htmlstr += '<label for="end_date">End Date</label>'
htmlstr += '<input type="text" name="end_date" />'
htmlstr += '<br> </br>'
htmlstr += '<input type="submit" value="Add Event" />'
htmlstr += '</form>'
return htmlstr;
// in your code I dont see where you ever do anything with `htmlstr `.
// Since you made `calendar_eda_add_form` a function, I'm assuming you meant to return `htmlstr`
// and insert the created html into your `insert_calendar_eda_form` div
// im assuming you have a reason for `clicked_id, arraylength` here but not sure what they are for
}
$(document).ready(function(){
//change this line to add the `#` and bind to `document`
$(document).on('submit','#calendar_eda_add_form',function(event){
event.preventDefault();
somefunction();
});
//put here to simulate adding form later
$('#create').click(function(){
//insert the created html into your `insert_calendar_eda_form` div
$('#insert_calendar_eda_form').html(calendar_eda_add_form);
});
});
function somefunction(){
alert ('clicked add event');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="create" value="create form"/>
<div class = "setup_eda_class" id = "Setup_eda">
<p>
<a> <span id="calendar_eda_form_top_x"> x </span> </a>
<br> </br>
<div id="insert_calendar_eda_form"> </div>
</p>
</div>
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 停止jquery中元素上的事件传播
- 同一元素上的多个事件,停止立即传播
- 如果父元素具有 preventDefault,如何将单击传播到子元素
- 棱角分明如何防止点击事件传播到日期选取器元素
- 使用鼠标滚轮滚动元素时传播的事件是什么
- 使用引导模式防止父 onclick 事件从子元素传播
- 无法为(稍后的)javascript生成的元素传播事件侦听器(提交表单)
- 停止在活动元素上传播
- 仅追加一次 UI 元素,避免事件传播
- 当我在表元素上滚动时,停止离子滚动上的传播
- 手动触发的JavaScript事件未针对iFrame上下文内的元素传播
- 如何停止事件传播到具有相同类的其他元素
- 当调用jQuery's.trigger()函数到绑定到该元素的其他处理程序的传播停止
- Javascript阻止事件通过元素传播
- 如何为与其他元素重叠但不是其子元素的元素停止传播
- 使用jQuery将所有事件从一个元素传播到另一个元素
- jQuery Draggable Stop:停止元素传播的函数
- 停止传播到附加的元素