【问题标题】:Add event when click at some place单击某个位置时添加事件
【发布时间】:2020-12-07 18:46:44
【问题描述】:

我正在开发一个简单的管理系统。现在我想让管理员更容易编辑数据。我的目标是,当一个元素被双击时,它会变成输入。然后在编辑之后,如果管理员在输入之外单击,它会运行一个事件来运行 ajax。就像PHPMyAdmin中的系统,当我们要编辑数据时,只需双击数据即可。

HTML

<tbody>
    <tr>
        <th scope="row">1</th>
        <td class="editable" name="email">myname@myweb.com</td>
        <td class="editable" name="username">this_is_username</td>
        <td class="editable" name="name">My Name</td>
    </tr>
</tbody>

JavaScript

$(".editable").dblclick(function () {
    const id = $(this).data("id");
    const col = $(this).attr("name");
    $(this).html(
        "<form>
             <input class="editing" type='text' value='" + $(this).val() + "'>
        </form>"
    );
});

// I want this Ajax will execute whene other side is clicked

$.ajax({
        url: "http://localhost/myProject/myController/myMethod",
        type: "post",
        data: {
            id: id,
            col: col,
            value: $('.editing').val()
        },
        success: function () {
            document.location.href = "http://localhost/myProject/myController";
        },
    });

控制器

public function myMethod()
{
    $id = $this->input->post("id");
    $col = $this->input->post("col");
    $value = $this->input->post("value");
    $this->db->set($col, $val);
    $this->db->where('id', $id);
    $this->db->update("my_table");
}

但是当管理员在输入之外单击时,我不知道如何创建一个事件来执行 Ajax。任何人都可以帮助我吗?还是做了什么功能?

***注意:我想在标记锚点上排除它。因此,当某个链接被按下时,它不会执行 Ajax,而是转到该链接。

【问题讨论】:

    标签: javascript html jquery ajax codeigniter


    【解决方案1】:

    使用输入元素的 onblur 事件。当用户从输入字段中移出焦点时,它将触发。

    【讨论】:

      【解决方案2】:

      由于您使用的是 jQuery,因此将 on blur 事件挂钩,如下所示:

          $(".editable").dblclick(function () {
              const id = $(this).data("id");
              const col = $(this).attr("name");
              $(this).html(
                  "<form>
                       <input class="editing" type='text' value='" + $(this).val() + "'>
                  </form>"
              );
              
              // use more specific targets here, i just use the class of the input
      // you should use the same $(this) content in the end
              $('.editing').blur(function() {
                  $.ajax({
                      url: "http://localhost/myProject/myController/myMethod",
                      type: "post",
                      data: {
                          id: id,
                          col: col,
                          value: $('.editing').val()
                      },
                      success: function () {
                          document.location.href = "http://localhost/myProject/myController";
                      },
                  });
              });
              
          });
      

      当您附加它的元素失去焦点时会触发 blur 事件,但不会通过冒泡触发,仅针对特定元素。

      查找blurfocusOut 的更多信息

      【讨论】:

      • .blur() 可以为锚标签例外吗?
      • 主播“破例”是什么意思?我不确定您想如何处理它们,但关键是了解您正在使用的选择器和您拥有的元素。解释得更好一点,我可能会提供更多帮助
      • 我明白了,我得到了我想要的东西
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多