简体   繁体   English

jQuery获取最接近的元素并过滤子级

[英]JQuery getting the closest element and filtering children

I have 3 forms with IDs ( form1 , form2 , form 3). 我有3个具有ID的表单(form1,form2,form 3)。 every form has its own different drop down lists (drop1, drop2) and submit button. 每个表单都有自己不同的下拉列表(drop1,drop2)和“提交”按钮。 When you click submit button in every form . 当您单击每种形式的“提交”按钮时。 It checks which option (in the drop down list of the same form) is selected . 它检查选择了哪个选项(在同一表单的下拉列表中)。 To find the exact selected Option I use this code : 为了找到确切的选择选项,我使用以下代码:

    $(".submitButton").each(function(){

          $( this ).click(function(){buttonAddClicked(1)});
    });


    function buttonAddClicked(FormID){
          $('form'+ FormID +' *').filter('.MyDropDownList').each( function(){

           var selOption=$(this).find('option:selected');
    }

Till now everything is working fine. 到现在为止一切正常。 I get the selected option with no problems at all.But, when I make some modifications problems happens. 我得到的选择选项完全没有问题。但是,当我进行一些修改时,就会发生问题。

On document ready, I copy form1 html . 准备好文档后,我复制form1 html。 So, form1 is duplicated with the same id. 因此,form1与相同的id重复。

--Let's say that I have form1A , form1B-- -假设我有form1A,form1B--

When I press submit button in form1A or form1B the code always go to the selected option in form1A. 当我在form1A或form1B中按Submit按钮时,代码总是转到form1A中的选定选项。 This problem is killing me . 这个问题使我丧命。

How can I modify my code to catch the form closest form . 如何修改代码以捕获最接近的表单。 I tried some techniques but didn't work . 我尝试了一些技巧,但是没有用。 The problem is in using ('*') with filter with closest. 问题在于将('*')与最近的过滤器一起使用。 Or if There's another solution to solve this I would be very thankful for you guys . 或者,如果还有另一种解决方案可以解决这个问题,我将非常感谢你们。 Thank you . 谢谢 。

Edit: I can Get the closest form easily , but how to loop (each) on that form Drop-Down-Lists, 编辑:我可以轻松获取最接近的表单,但是如何在该表单上循环(每个)下拉列表,

The problem is for all button clicks you are passing 1 as the form id. 问题是您要传递1作为表单ID的所有按钮单击。

There is no need for that, assuming the button is within the form then, you can use .closest() to find the form which contains the button then use .find() to find the select element and .val() to find its selected value as shown below 不需要这样做,假设按钮位于表单内,则可以使用.closest()查找包含按钮的表单,然后使用.find()查找select元素和.val()查找其元素选择值如下图所示

jQuery(function () {
    $(".submitButton").click(function () {
        var $form = $(this).closest('form');
        var selectedValue = $form.find('.MyDropDownList').val();
        //do whatever you want to do with the selectedvalue

        //if you have multiple elements and wants to have an array of values
        var selectedValues = $form.find('.MyDropDownList').map(function () {
            return $(this).val()
        }).get();
    });
})

First, you should never have duplicate IDs in the document. 首先,您在文档中永远不应有重复的ID。 It causes problems with assistive technology. 它会导致辅助技术出现问题。 So the best solution is to modify the ID when you duplicate the form. 因此,最好的解决方案是在复制表单时修改ID。

However, you can use jQuery's closest to find the closest parent http://api.jquery.com/closest/ 但是,您可以使用jQuery closest来找到最接近的父代http://api.jquery.com/closest/

function buttonAddClicked(){
    var selOption = $(this).closest('form').find('.MyDropDownList').val();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM