简体   繁体   English

此HTML的CSS选择器和Jquery

[英]Css Selector and Jquery for this html

I have the next HTML: 我有下一个HTML:

 <div class="grid_4 col" id="countries">
            <div class="header">COUNTRIES</div>
            <div class="scroll-area">
                <ul id="countrySelector">

                <li><input type="checkbox" value="3" name="0" id="p0"> Algeria</li>
                <li><input type="checkbox" value="3" name="0" checked="true" id="p1"> Angola</li>
                <li><input type="checkbox" value="3" name="0" checked="true" id="p2"> Benin</li>
                <li><input type="checkbox" value="3" name="0" checked="true" id="p3"> Botswana</li>
                <li><input type="checkbox" value="3" name="0" checked="true" id="p4"> Burkina Faso</li>
                <li><input type="checkbox" value="3" name="0" id="p5"> Burundi</li>
                <li><input type="checkbox" value="3" name="0" id="p6"> Cameroon</li>
                <li><input type="checkbox" value="3" name="0" id="p7"> Cape Verde</li>
                <li><input type="checkbox" value="3" name="0" id="p8"> Central African Republic</li>
                <input type="checkbox" value="2" name="3" id="p142"> Tuvalu</li>
                <li><input type="checkbox" value="2" name="3" id="p143"> Vanuatu</li>
                <li><input type="checkbox" value="2" name="3" id="p144"> Wallis and Futuna</li>
            </div>                
        </div>

I would like to: - I Expect to get in an array all the inputs id (p143,p144) in an array using the name attribute (3 in this case). 我想:-我希望使用name属性(在本例中为3)在数组中获取所有输入id(p143,p144)。

  • Then I would like to remove the LI elements that contains that inputs. 然后,我想删除包含该输入的LI元素。 I know that if I get the input elemens by a CSS Selector I would do it using $"CSSSELECTOR".parent().remove(); 我知道,如果我通过CSS选择器获得输入元素,则可以使用$“ CSSSELECTOR” .parent()。remove();来完成。

And in a final stage, how to do the change to that CSS Selector to get only the Checked=True using the name again (I want again the IDs). 在最后阶段,如何更改该CSS选择器,以再次使用该名称(仅再次输入ID)来获取Checked = True。

For the second I´m trying something like: 对于第二个我正在尝试类似的东西:

jQuery("ul#countrySelector > li > checkbox[name=\"" + index + "\"]").parent("li").remove();

Where index is the 0 or 3 in this case, but it doesn´t work. 在这种情况下,其中index是0或3,但是它不起作用。

I don´t need the full solution, and explanation would be better to start thinking in css selectors. 我不需要完整的解决方案,开始在CSS选择器中进行思考会更好。

Thanks. 谢谢。

SOLVED. 解决了。

The HTML was not right because I copy it bad, it is generate by other events. HTML是不正确的,因为我将其复制不好,它是由其他事件生成的。

I only mark as correct the first answer (Wirey) because the others use EACH method and I think that the performance of Wirey solution is the best, and this HTML is just a part of all the generate code in a few levels. 我只将第一个答案(Wirey)标记为正确,因为其他答案都使用EACH方法,并且我认为Wirey解决方案的性能是最好的,并且该HTML只是几个级别中所有生成代码的一部分。

Once you fix your markup with the missing <li> and </ul> .. 使用缺失的<li></ul>修复标记后。

First issue is your selector 首先是您的选择器

jQuery("ul#countrySelector > li > checkbox[name=\"" + index + "\"]")
// it says starting from ul with id=countrySelector > find children element li > find children element checkbox

It's not a checkbox element but an input element of type checkbox so what you need is 它不是复选框元素,而是复选框类型的输入元素,所以您需要的是

jQuery("ul#countrySelector > li > input[type=checkbox][name=\"" + index + "\"]")
// or just jQuery("ul#countrySelector > li > input[name=\"" + index + "\"]") if these are the only elements with that name

You state that you want an array with those elements ids. 您声明要具有这些元素ID的数组。 So you can use jQuery's .map() function to return that in a collection then .get() to get an array 因此,您可以使用jQuery的.map()函数将其返回到集合中,然后使用.get()获取数组

var ids = jQuery("ul#countrySelector > li > input[type=checkbox][name=\"" + index + "\"]").map(function(){
    return this.id;
}).get(); // <-- use get() to get id's back as an array - now ids contains all the id's in an array

If you wanted to get only checked items, you can use the :checked selector to get only checked inputs 如果只想获取选中的项目,可以使用:checked选择器仅获取选中的输入

jQuery("ul#countrySelector > li > input[type=checkbox][name=\"" + index + "\"]:checked")

Once you get your selector, you can just find your li just like in your example and remove 找到选择器后,就可以像在示例中一样找到您的li ,然后将其删除

$(yourselector).parent('li').remove();
// or if it's nested deeper $(yourselector).closest('li').remove();

Really hard to understand what you want to do, but here is an example for removing a LI . 确实很难理解您想做什么,但是这里有一个删除LI的示例。 First, your HTML is not proper, here it is fixed: 首先,您的HTML不正确,此处已修复:

<div class="grid_4 col" id="countries">
    <div class="header">COUNTRIES</div>
    <div class="scroll-area">
        <ul id="countrySelector">
            <li><input type="checkbox" value="3" name="0" id="p0"> Algeria</li>
            <li><input type="checkbox" value="3" name="0" checked="true" id="p1"> Angola</li>
            <li><input type="checkbox" value="3" name="0" checked="true" id="p2"> Benin</li>
            <li><input type="checkbox" value="3" name="0" checked="true" id="p3"> Botswana</li>
<li><input type="checkbox" value="3" name="0" checked="true" id="p4"> Burkina Faso</li>
            <li><input type="checkbox" value="3" name="0" id="p5"> Burundi</li>
            <li><input type="checkbox" value="3" name="0" id="p6"> Cameroon</li>
            <li><input type="checkbox" value="3" name="0" id="p7"> Cape Verde</li>
            <li><input type="checkbox" value="3" name="0" id="p8"> Central African Republic</li>
        <li><input type="checkbox" value="2" name="3" id="p142"> Tuvalu</li>
            <li><input type="checkbox" value="2" name="3" id="p143"> Vanuatu</li>
            <li><input type="checkbox" value="2" name="3" id="p144"> Wallis and Futuna</li>
        </ul>
    </div>                
</div>

Will edit with JQuery example. 将使用JQuery示例进行编辑。

if their attribute is '0': 如果其属性为“ 0”:

var countryArray = new Array();
$('#countries input[type=checkbox]').each(function(){
    if ($(this).attr('name') == '0') {
        countryArray.push($(this).attr('id'));
        $(this).parent('li').remove();
    }
})
console.log(countryArray)

if their attribute is '0' and they are checked: 如果它们的属性为“ 0”,并进行了检查:

var countryArray = new Array();
$('#countries input[type=checkbox]:checked').each(function(){
    if ($(this).attr('name') == '0') {
        countryArray.push($(this).attr('id'));
        $(this).parent('li').remove();
    }
})
console.log(countryArray)

Is this what you are looking for? 这是你想要的? This will iterate through all the checkboxes and store the details in an array. 这将遍历所有复选框,并将详细信息存储在数组中。

Sample JSFiddle : http://jsfiddle.net/Y84vr/32/ 示例JSFiddle: http : //jsfiddle.net/Y84vr/32/

$(document).ready(function(){
    var myArray = createArray(20,3);
    var i=0;
    $('input[type="checkbox"]').each(function(){
        var isChecked = false;
        if ($(this).is('[checked]')) {
            isChecked = true;
        }    
        myArray[i][0] = $(this).attr('id');
        myArray[i][1] = $(this).attr('name');
        myArray[i][2] = isChecked;
        i++;
    });
    for(var c=0;c<15;c++){
       $('#output').append(myArray[c][0] + "  -  " + myArray[c][1] + "  -  " + myArray[c][2] + "<br/>");
    }
});

function createArray(dimX,dimY){
    var arr = [];
    for(var x = 0; x < dimX; x++){
       arr[x] = [];    
       for(var y = 0; y < dimY; y++){
          arr[x][y] = '';    
       }    
    }  
    return arr;
}

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

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