选择在Javascript中提交时分配的项目类
Select item class which has been assigned on submit in Javascript
嗨,我正在尝试选择一个在jQuery提交时分配的类。我无法选择班级。
$('form.ajax').on('submit', function(e) {
$.ajax({
success: function(data) {
$("#" + key).addClass("missed");
});
$('input.missed').click(function() {
alert("Clicked");
});
这是我努力实现目标的一个例子。
假设问题是"如何在提交事件后向元素添加点击处理程序?"那么答案可能是您的成功回调有几个语法错误,并且您的缩进表明您管理DOM的顺序不正确。这里举例说明一些注释代码:
$('form.ajax').on('submit', function(e) {
// Prevent an actual submission in favor of AJAX
e.preventDefault();
// Spawn an AJAX request to http://ecample.com/api/form_validator
$.ajax({url: 'http://example.com/api/form_validator'})
// Once we have a success response *then* do the following.
.then(function(response) {
// Use the response from the server to iterate of the keys array.
$.each(response.keys, function(i, key) {
// add the missing class to the element that matches the key.
$('#element-' + key).addClass('missed');
});
});
});
// Assign a click event to the entire form but filter it by 'input.missing'
// this way whe an input does have the missingf class the click will happen
// otherwise it is ignored. This is call event delegation.
$('form.ajax').on('click', 'input.missed', function(e) {
alert('Clicked');
});
这是呼叫事件委派。
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 在循环中分配json值时,值被覆盖
- 正在将数据主题添加到所有项目
- 动态分配GA增强型电子商务跟踪器
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何检查管道中未定义的项目
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 学生搜索项目jquery/javascript
- 如何获取不属于我项目的上一页的URL
- 如何在jQuery中将函数的输出分配给变量
- 为集合分配大量的模型弹药
- onclick函数需要双击,因为类分配延迟
- Dojo:访问dijit.form.Select中单击的项目
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 选择在Javascript中提交时分配的项目类
- 无法为项目分配第二个项目
- 循环遍历数组中的项目以分配 data-id,但仅获取数组中的最后一项
- D3:如何分配大小,颜色的项目(碰撞检测可视化)
- 如何删除选择标签的所有选项,并在javascript中分配一个新的项目列表来选择选项