当前位置: 首页 > 知识库问答 >
问题:

当我在jQuery中通过克隆方法创建一个新的时,如何触发删除按钮?

司徒啸
2023-03-14

任务是,当我点击加号按钮添加一个新的搜索输入,它必须添加一个加号按钮和删除按钮(它叫'Times-BTN')的新字段,所有的工作都很好,但当我点击删除按钮,第一个创建的只是删除,但当我点击任何其他按钮,它都不起作用。那么,我怎样才能触发移除按钮呢?

search.html

    <!-- Search formsets -->
    <div class="search-field">
        <h2 class="show"></h2>
        <form method="get" id="main_search">
            <div id="first_search_group" class="form-group row search_repeated search_group">
                <div class="col-lg-2 d-inline p-2 word_repeated">
                    <label style="font-size: large; padding-top: 5px">Sentence or Word</label>
                </div>
                <!-- Filter -->
                <div class="col-lg-2 d-inline p-2">
                    <select name="formsets_option" id="formsets_option" class="form-control">
                        {% if formsets_option == 'contains' %}
                        <option id="contains" value="contains" selected>contains</option>
                        {% else %}
                        <option id="contains" value="contains">contains</option>
                        {% endif %}

                        {% if formsets_option == 'start_with' %}
                        <option id="start_with" value="start_with" selected>start with</option>
                        {% else %}
                        <option id="start_with" value="start_with">start with</option>
                        {% endif %}

                        {% if formsets_option == 'is' %}
                        <option id="is" value="is" selected>is</option>
                        {% else %}
                        <option id="is" value="is">is</option>
                        {% endif %}

                        {% if formsets_option == 'end_with' %}
                        <option id="end_with" value="end_with" selected>end with</option>
                        {% else %}
                        <option id="end_with" value="end_with">end with</option>
                        {% endif %}
                    </select>
                </div>
                <div class="col-lg-4 d-inline p-2">
                    <input id="search_btn" type="text" class="form-control"
                           onkeyup="if (this.value != ''){document.getElementById('clear').hidden=false} else {document.getElementById('clear').hidden=true}"
                           placeholder="search" name="query" value="{{ request.GET.query }}">
                </div>
                <div class="col-lg-2 d-inline p-2">
                    <select name="verb_option" id="verb_option" class="form-control">
                        {% if verb_option == '' %}
                        <option value="" selected>Any POS</option>
                        {% else %}
                        <option value="">Any POS</option>
                        {% endif %}

                        {% if verb_option == 'Adj' %}
                        <option value="Adj" selected>Adjective</option>
                        {% else %}
                        <option value="Adj">Adjective</option>
                        {% endif %}

                        {% if verb_option == 'Adv' %}
                        <option value="Adv" selected>Adverb</option>
                        {% else %}
                        <option value="Adv">Adverb</option>
                        {% endif %}

                        {% if verb_option == 'Asp' %}
                        <option value="Asp" selected>Aspect Marker</option>
                        {% else %}
                        <option value="Asp">Aspect Marker</option>
                        {% endif %}

                        {% if verb_option == 'Cl' %}
                        <option value="Cl" selected>Classifier</option>
                        {% else %}
                        <option value="Cl">Classifier</option>
                        {% endif %}

                        {% if verb_option == 'Conj' %}
                        <option value="Conj" selected>Conjunction</option>
                        {% else %}
                        <option value="Conj">Conjunction</option>
                        {% endif %}

                        {% if verb_option == 'Det' %}
                        <option value="Det" selected>Determiner</option>
                        {% else %}
                        <option value="Det">Determiner</option>
                        {% endif %}

                        {% if verb_option == 'Idiom' %}
                        <option value="Idiom" selected>Idiom</option>
                        {% else %}
                        <option value="Idiom">Idiom</option>
                        {% endif %}

                        {% if verb_option == 'Intj' %}
                        <option value="Intj" selected>Interjection</option>
                        {% else %}
                        <option value="Intj">Interjection</option>
                        {% endif %}

                        {% if verb_option == 'Noun' %}
                        <option value="Noun" selected>Noun</option>
                        {% else %}
                        <option value="Noun">Noun</option>
                        {% endif %}

                        {% if verb_option == 'Num' %}
                        <option value="Num" selected>Number</option>
                        {% else %}
                        <option value="Num">Number</option>
                        {% endif %}

                        {% if verb_option == 'Ono' %}
                        <option value="Ono" selected>Onomatopoeia</option>
                        {% else %}
                        <option value="Ono">Onomatopoeia</option>
                        {% endif %}

                        {% if verb_option == 'Part' %}
                        <option value="Part" selected>Particle</option>
                        {% else %}
                        <option value="Part">Particle</option>
                        {% endif %}

                        {% if verb_option == 'Prep' %}
                        <option value="Prep" selected>Preposition</option>
                        {% else %}
                        <option value="Prep">Preposition</option>
                        {% endif %}

                        {% if verb_option == 'Pro' %}
                        <option value="Pro" selected>Pronoun</option>
                        {% else %}
                        <option value="Pro">Pronoun</option>
                        {% endif %}

                        {% if verb_option == 'Pu' %}
                        <option value="Pu" selected>Punctuation</option>
                        {% else %}
                        <option value="Pu">Punctuation</option>
                        {% endif %}

                        {% if verb_option == 'SFP' %}
                        <option value="SFP" selected>Sentence-Final Particle</option>
                        {% else %}
                        <option value="SFP">Sentence-Final Particle</option>
                        {% endif %}

                        {% if verb_option == 'Verb' %}
                        <option value="Verb" selected>Verb</option>
                        {% else %}
                        <option value="Verb">Verb</option>
                        {% endif %}

                        {% if verb_option == 'X' %}
                        <option value="X" selected>Unclassified</option>
                        {% else %}
                        <option value="X">Unclassified</option>
                        {% endif %}
                    </select>
                </div>
                <div style="align-self: center;font-size: 14px;color: #2f855a;" class="btn-plus col-lg-1 d-inline">
                    <div class="fa fa-plus d-inline"></div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-md-12">
                    <input type="submit" name="main_search" value="Search" class="btn btn-primary">
                    <a href="{% url 'search:search' %}" style="color: #c53030; margin-left: 10px;border: 0; background-color: transparent;cursor: pointer" id="clear" hidden>Clear Search</a>
                </div>
            </div>
        </form>
    </div>

search.html

<script type="text/javascript">
    $(document).ready(function()
    {
        let btn_plus = $(".btn-plus");
        let search_group = $(".search_group")
        let search_form = $("#main_search")

        let create_times_parent = document.createElement("div")
            create_times_parent.classList.add("btn-times", "col-lg-1", "d-inline")
        let create_times = document.createElement("div")
            create_times.classList.add("fa", "fa-times", "d-inline")
        create_times_parent.appendChild(create_times)

        function add_inc()
        {
            btn_plus.click(function ()
            {
                $("div.search_group:first-of-type").after(search_group.clone(true, true));
                search_group.addClass('created')
                search_group.append(create_times_parent)

                if ($(this).data('clicked', true)) {
                    create_times_parent.onclick = function ()
                    {
                        $(this).parent('.created').remove()
                        console.log("clicked")
                    }
                }
            });

        }
        add_inc()
    });
    </script>

这是一个解释任务输入图像描述的屏幕

共有1个答案

花俊雄
2023-03-14

您的div是动态生成的,因此只需将其与DOM中已经存在的静态元素绑定即可。

此外,我将整个删除事件移到函数之外,然后使用$(document).On(“Click”,“.remove”,function(){..这将触发删除按钮,然后使用.Closter(“.Created”).remove()删除整个div。

演示代码:

null

$(document).ready(function() {
  let search_group = $(".search_group")
  let search_form = $("#main_search")

  let create_times_parent = document.createElement("div")
  create_times_parent.classList.add("btn-times", "col-lg-1", "d-inline")

  let create_times = document.createElement("div")
  //added on extra class here..
  create_times.classList.add("fa", "fa-times", "d-inline", "remove")
  create_times_parent.appendChild(create_times)

  $(document).on('click', '.btn-plus', function() {
    $("div.search_group:first-of-type").after(search_group.clone(true, true));
    search_group.addClass('created')
    search_group.append(create_times_parent)

  });

  //onclick of remove..
  $(document).on("click", ".remove", function() {
    $(this).closest('.created').remove() //get closest outer div
    console.log("clicked")
  })

})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="search-field">
  <h2 class="show"></h2>
  <form method="get" id="main_search">
    <div id="first_search_group" class="form-group row search_repeated search_group">
      <div class="col-lg-2 d-inline p-2 word_repeated">
        <label style="font-size: large; padding-top: 5px">Sentence or Word</label>
      </div>
      <!-- Filter -->
      <div class="col-lg-2 d-inline p-2">
        <select name="formsets_option" id="formsets_option" class="form-control">

          <option id="contains" value="contains" selected>contains</option>

          <option id="start_with" value="start_with" selected>start with</option>

          <option id="is" value="is" selected>is</option>

          <option id="end_with" value="end_with" selected>end with</option>

        </select>
      </div>
      <div class="col-lg-4 d-inline p-2">
        <input id="search_btn" type="text" class="form-control" onkeyup="if (this.value != ''){document.getElementById('clear').hidden=false} else {document.getElementById('clear').hidden=true}" placeholder="search" name="query" value="{{ request.GET.query }}">
      </div>
      <div class="col-lg-2 d-inline p-2">
        <select name="verb_option" id="verb_option" class="form-control">

          <option value="" selected>Any POS</option>

        </select>
      </div>
      <div style="align-self: center;font-size: 14px;color: #2f855a;" class="btn-plus col-lg-1 d-inline">
        <div class="fa fa-plus d-inline"></div>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-12">
        <input type="submit" name="main_search" value="Search" class="btn btn-primary">
        <a href="{% url 'search:search' %}" style="color: #c53030; margin-left: 10px;border: 0; background-color: transparent;cursor: pointer" id="clear" hidden>Clear Search</a>
      </div>
    </div>
  </form>
</div>
 类似资料:
  • 这是我的代码: 我所要做的就是使esc按钮与backspace按钮完全相同。所以我想删除

  • 我正在开发一个flappy bird克隆,我真的被我想要创建的按钮卡住了。对不起,我知道这是一个基本问题,但我是初学者,我真的需要你的帮助。所以,游戏已经基本完成了,但是我想做一个按钮来做同样的事情,而不是触摸整个屏幕让一只鸟跳起来。这是我的InPurthandler课程: 我在GameScreen类中创建了一个舞台和一个演员(按钮)(它不工作),并在屏幕上显示,但我不知道下一步该怎么做。再一次,

  • 问题内容: 我正在将datatables插件与服务器端数据一起使用,并使用AJAX更新表。 我的dataTables设置如下所示: 我有一些用于过滤数据服务器端的自定义字段,我将其与AJAX请求一起推送。问题是,我不知道如何从表外部触发JSON请求。如果用户键入过滤器,则fnServerData将触发并更新表。但是,如果用户在表外选择控件,则我不知道如何触发 fnServerData 函数。 现在

  • 问题内容: 我知道我可以使用它来创建DDL创建触发器; 问题在于,该触发器将在“创建序列”之类的DDL上运行;如何仅对“创建表” DDL执行此操作? 问题答案: CREATE OR REPLACE TRIGGER create_table_trigger AFTER CREATE ON SCHEMA BEGIN IF SYS.DICTIONARY_OBJ_TYPE = ‘TABLE’ THEN .

  • 我有一个平面JS对象: 我想克隆除一个元素以外的对象: 最简单的方法是什么(如果可能的话更倾向于使用ES6/7)?

  • while(true)连续传递这些指令,但仅当accept()方法单击时才创建线程? 新线程是否有专用端口?套接字通信不是一对一的吗? 软件如何跟踪生成的套接字线程,这实际上有关系吗? 如何向刚写给我的线程发送答复? 也许是因为我缺乏谷歌技能,但我找不到一个好的教程来做这件事:帮助?