简体   繁体   English

针对jQuery中的最后一个孩子

[英]target the last child in jquery

I have created a jquery questionnaire, when the user clicks on the radio button it navigates to the next question click here . 我创建了一个jquery问卷,当用户单击单选按钮时,它导航到下一个问题, 请单击此处 On the last question a submit button is visible, however if you click on the the last radio button all content disappears. 在最后一个问题上,可以看到一个提交按钮,但是,如果单击最后一个单选按钮,则所有内容都将消失。 Is there a way to deactivate the next() function when the user gets to the last question. 当用户到达最后一个问题时,是否有一种方法可以停用next()函数。

            $('.responsible-gaming__body input').click(function () {
            selfAssessmentTotal += parseInt($(this).val());
            var parentContainer = $(this).parents('.responsible-gaming__parentContainer');
            parentContainer.hide();
            parentContainer.next('.responsible-gaming__parentContainer').show();
        });

use next('.responsible-gaming__parentContainer').length to see the next element existance. 使用next('.responsible-gaming__parentContainer').length查看下一个元素的存在。 If it is then execute the next click code else do nothing: 如果是,则执行下一个单击代码,否则不执行任何操作:

if(parentContainer.next('.responsible-gaming__parentContainer').length){
   //next element exist....add code here
}

complete code: 完整的代码:

$('.responsible-gaming__body input').click(function () {
        selfAssessmentTotal += parseInt($(this).val());
        var parentContainer = $(this).parents('.responsible-gaming__parentContainer');
        if(parentContainer.next('.responsible-gaming__parentContainer').length){
           parentContainer.hide();
           parentContainer.next('.responsible-gaming__parentContainer').show();
        }
 });
if(!parentContainer.next('.responsible-gaming__parentContainer').length){
//do your stuff here
}
if(parentContainer.next('.responsible-gaming__parentContainer').length){
       parentContainer.hide();
       parentContainer.next('.responsible-gaming__parentContainer').show();
 }

DEMO 演示

Change your selector for click function to 将选择器的单击功能更改为

$('.responsible-gaming__parentContainer:not(:last-child) input')

instead of 代替

$('.responsible-gaming__body input')

This makes your code become simpler to: 这使您的代码变得更简单:

$('.responsible-gaming__parentContainer:not(:last-child) input').click(function () {
  selfAssessmentTotal += parseInt($(this).val());
  $(this).parent().parent().hide();
  $(this).parent().parent().next('.responsible-gaming__parentContainer').show();
});

Only hide the parentContainer once the next container is shown (this won't happen if there isn't a next container): 仅在显示下一个容器后才隐藏parentContainer(如果没有下一个容器,则不会发生这种情况):

$('.responsible-gaming__body input').click(function () {
    selfAssessmentTotal += parseInt($(this).val());
    var parentContainer = $(this).parents('.responsible-gaming__parentContainer');
    parentContainer.next('.responsible-gaming__parentContainer').show(0, function() {
        parentContainer.hide();
});

JSFiddle JSFiddle

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

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