简体   繁体   English

如何获得ID名称的子字符串?

[英]How can I get a substring of an ID name?

I have a number of buttons within a section, each with an id of the form #balls-left-n , where n ranges from 1 to 15. 我在一个部分中有许多按钮,每个按钮的ID形式为#balls-left-n ,其中n的范围为1到15。

When one of these buttons is clicked, I want to grab the number from the id that was clicked and hide all of the buttons with ids that have names including numbers that are greater than the one clicked on. 当单击这些按钮之一时,我想从单击的ID中获取数字,并隐藏所有ID包含名称的数字包含大于单击的数字的按钮。

So, if #balls-left-13 is clicked, I want to hide #balls-left-14 and #balls-left-15 . 因此,如果单击#balls-left-13 ,我想隐藏#balls-left-14#balls-left-15 But if #balls-left-3 is clicked I want to hide all the buttons from #balls-left-4 through #balls-left-15 . 但是,如果单击#balls-left-3 ,我想隐藏#balls-left-4#balls-left-15所有按钮。

I'm a novice at web-dev so if I've made other mistakes or taken a poor approach don't hesitate to point that out. 我是web-dev的新手,所以如果我犯了其他错误或采取了错误的方法,请毫不犹豫地指出。

I have a handler for each of the buttons (which if I knew more could probably be one function) that look like this: 我为每个按钮都有一个处理程序(如果我知道更多,可能是一个函数),如下所示:

$("#balls-left-14").click(function() {
    var num_balls = $(this).attr('id').match(/[\d]/);
    j_end_balls_on_table = 14;
    $("#balls-left button:gt(num_balls-2)").hide;
    ...
    other stuff
    ...
});

This didn't work and I get an error that num_balls is undefined , which I don't understand. 这没有用,并且我得到一个错误, num_ballsundefined ,我不明白。 #balls-left is the section all of the buttons are inside of. #balls-left是所有按钮都位于其中的部分。

relevant HTML as requested 根据要求提供相关HTML

<section id="balls-left">
    <h2>How Many Balls are Left on the Table?</h2>          
    <button type="button" id="balls-left-2" class="x-balls-left">2</button>
    <button type="button" id="balls-left-3" class="x-balls-left">3</button>
    <button type="button" id="balls-left-4" class="x-balls-left">4</button>
    <button type="button" id="balls-left-5" class="x-balls-left">5</button>
    <button type="button" id="balls-left-6" class="x-balls-left">6</button>
    <button type="button" id="balls-left-7" class="x-balls-left">7</button>
    <button type="button" id="balls-left-8" class="x-balls-left">8</button>
    <button type="button" id="balls-left-9" class="x-balls-left">9</button>
    <button type="button" id="balls-left-10" class="x-balls-left">10</button>
    <button type="button" id="balls-left-11" class="x-balls-left">11</button>
    <button type="button" id="balls-left-12" class="x-balls-left">12</button>
    <button type="button" id="balls-left-13" class="x-balls-left">13</button>
    <button type="button" id="balls-left-14" class="x-balls-left">14</button>
    <button type="button" id="balls-left-15" class="x-balls-left">15</button>
</section>

Hope this helps. 希望这可以帮助。

var exploded = id.split("-");
alert(exploded.pop());

Now, to use that concept on your HTML structure, you can do something like this: 现在,要在HTML结构上使用该概念,您可以执行以下操作:

$(document).ready(function() {
    $(".x-balls-left").click(function(e) {
        e.preventDefault();
        var exploded = this.id.split("-");
        alert(exploded.pop());
    });
});

And here's a Fiddle you can play around with. 这是您可以玩的小提琴

 $(document).on('click', '.balls-left', function() { var num = getNum(this); $('.balls-left').each(function() { var that = $(this); var bnum = getNum(that); if (bnum > num) { that.show(); } else { that.hide(); } }); }); var getNum = function(elem) { if (elem) { return $(elem).attr('id').replace('balls-left-', ''); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="balls-left-1" class="balls-left">Ball 1</div> <div id="balls-left-2" class="balls-left">Ball 2</div> <div id="balls-left-3" class="balls-left">Ball 3</div> <div id="balls-left-4" class="balls-left">Ball 4</div> <div id="balls-left-5" class="balls-left">Ball 5</div> 

You might don't even need all of these if your elements to hide share the same parent. 如果要隐藏的元素共享同一个父元素,甚至可能不需要所有这些元素。 Just set class on click .selected and hide the rest using CSS .selected.x-balls-left ~ .x-balls-left {display: none;} 只需在click .selected上设置类,然后使用CSS .selected.x-balls-left ~ .x-balls-left {display: none;}隐藏其余部分.selected.x-balls-left ~ .x-balls-left {display: none;}

 $('.x-balls-left').click(function() { $(this).toggleClass('selected'); }) 
 .selected.x-balls-left ~ .x-balls-left { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section id="balls-left"> <h2>How Many Balls are Left on the Table?</h2> <button type="button" id="balls-left-2" class="x-balls-left">2</button> <button type="button" id="balls-left-3" class="x-balls-left">3</button> <button type="button" id="balls-left-4" class="x-balls-left">4</button> <button type="button" id="balls-left-5" class="x-balls-left">5</button> <button type="button" id="balls-left-6" class="x-balls-left">6</button> <button type="button" id="balls-left-7" class="x-balls-left">7</button> <button type="button" id="balls-left-8" class="x-balls-left">8</button> <button type="button" id="balls-left-9" class="x-balls-left">9</button> <button type="button" id="balls-left-10" class="x-balls-left">10</button> <button type="button" id="balls-left-11" class="x-balls-left">11</button> <button type="button" id="balls-left-12" class="x-balls-left">12</button> <button type="button" id="balls-left-13" class="x-balls-left">13</button> <button type="button" id="balls-left-14" class="x-balls-left">14</button> <button type="button" id="balls-left-15" class="x-balls-left">15</button> </section> 

$("#balls-left button:gt(num_balls-2)").hide;

This is an invalid CSS selector, and only gets the hide method, without calling it. 这是无效的CSS选择器,仅获取 hide方法,而不调用它。 You want something like: 您想要类似的东西:

$("#balls-left button:gt("+(num_balls-2)+")").hide();

First you should put a class on each object so you can reference them all at once, and the simplest way to understand is to just put the ball number right in the tag as a custom attribute if you can: 首先,您应该在每个对象上放置一个类,以便可以一次全部引用它们,而最简单的理解方法是,只要可以,就将球号直接放在标签中作为自定义属性:

<input type="button" id="balls-left-1" class="left-ball" num="1"/>
<input type="button" id="balls-left-2" class="left-ball" num="2"/>
etc...

Then you can write the javascript as follows: 然后,您可以编写如下的javascript:

$('.left-ball').click(function () {
    var BallNum = $(this).attr('num');

    $('.left-ball').each(function () {
        if ($(this).attr('num') > BallNum) {
            $(this).hide();
        }
    });
});

You can use RegEx match like this. 您可以像这样使用RegEx匹配。 This might resolve your undefined num_balls error message. 这可能会解决您未定义的num_balls错误消息。

$("#balls-left-14").click(function() {
    var ret = $(this).attr('id').match("[0-9]+");
    var  num_balls = ret[0];
    j_end_balls_on_table = 14;
    $("#balls-left button:gt(num_balls-2)").hide;
    ...
    other stuff
    ...
});

Another way of doing it using your original HTML: 使用原始HTML的另一种方法:

$('.x-balls-left').click(function () {
    var BallNum = $(this)[0].innerHTML;

    $('.x-balls-left').each(function () {
        if ($(this)[0].innerHTML > BallNum) {
            $(this).hide();
        }
    });
});

I just did it like this: 我只是这样做:

 $('button[id^=balls-left-]').click(function(){
     var num_balls = $(this).attr('id').match(/[\d]/);
     $('#balls-left button:gt(' + num_balls + ')').hide();
 });

Keep in mind that :gt select by index, it means that $('#balls-left button:gt(2)') will not select the button with id balls-left-2 but the one with id balls-left-4 (according to the html you posted). 请记住:gt按索引选择,这意味着$('#balls-left button:gt(2)')不会选择ID为balls-left-2的按钮,而是ID为balls-left-4的按钮。 (根据您发布的html)。

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

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