选择在Javascript中提交时分配的项目类

Select item class which has been assigned on submit in Javascript

本文关键字:分配 项目 提交 Javascript 选择      更新时间:2023-09-26

嗨,我正在尝试选择一个在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');
});

这是呼叫事件委派。