简体   繁体   English

如何获得 <li> 当里面的元素被点击时的元素?

[英]How to get index of <li> element when element inside it clicked?

I have step form with 3 steps. 我有3个步骤的步骤表。 I want to get index of <li> in stepinstallment when <label> inside it selected to check: 我想在分stepinstallment安装中选择<label>进行检查时获取<li>索引:

  1. If the first step is selected, it will change background color of circle class. 如果选择了第一步,它将更改circle类的背景颜色。

  2. If the second and third step is selected without first step, it will display error message. 如果选择了第二步和第三步而没有第一步,它将显示错误消息。

I've tried many ways on SO and write myself but it doesn't work. 我在SO上尝试了许多方法并写了自己,但没有用。

This is my code 这是我的代码

<ul class="clearfix">
    <li>
        <div class="step-one circle">
            <p>Step 1</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 2</p>
        </div>
    </li>
    <li>
        <div class="step-two circle">
            <p>Step 3</p>
        </div>
    </li>
</ul>

<div class="stepinstallment">
    <ul>
        <li id="step-one" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-two" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>

        <li id="step-three" class="step">
            <h2>Choose your document</h2>
            <div class="step-content">
                <label>
                    <input type="radio" id="option-1" name="options" value="1" />
                    <span>ID card and household registration</span>
                </label>
            </div>
        </li>
    </ul>
</div>

JS JS

var sCircle = $('.steps ul li');

    $('.step label').on('click', function(){
        var $this = $(this),
            sCircleIndex = parseInt(sCircle.index()),
            sCircleChild = sCircle.children('div'),
            currParent = $this.parents('ul').index();

        console.log(currParent);
    });

Above JS code always return 0 . 上面的JS代码始终返回0 Hope anyone can figure me out this case. 希望任何人都能弄清楚我的情况。 Thanks in advance. 提前致谢。

Try using .closest() 尝试使用.closest()

$(".stepinstallment label").on("click", function() {
    console.log($(this).closest("li").index())
});

  $('.stepinstallment label').on('click', function(){ console.log($(this).closest("li").index()) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="clearfix"> <li> <div class="step-one circle"> <p>Step 1</p> </div> </li> <li> <div class="step-two circle"> <p>Step 2</p> </div> </li> <li> <div class="step-two circle"> <p>Step 3</p> </div> </li> </ul> <div class="stepinstallment"> <ul> <li id="step-one"> <h2>Choose your document</h2> <div class="step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> <li id="step-two"> <h2>Choose your document</h2> <div class="step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> <li id="step-three"> <h2>Choose your document</h2> <div class="step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> </ul> </div> 

I hope this example will help you out !!! 我希望这个例子对您有帮助!

 $("ul#wizard li").click(function () { var index = $("ul#wizard li").index(this); if(index!=0) alert("error ") else alert("index is: " + index) }); 
 <ul id="wizard"> <li>Step 1</li> <li>Step 2</li> <li>Step 3</li> <p>Some random tag</p> </ul> 

$('.step label')将不返回任何元素,因为没有与css选择器匹配的html

I can't see the .steps and .step class in your html. 我看不到.steps.step在你的HTML类。

with your html, I guess this code will work 与您的HTML,我想这段代码将工作

$('.stepinstallment').on('click', 'label', function (e) {
    var $this = $(this);
    var li = $this.parents('li');
    console.log(li.index());
});

the steps of the code: 代码的步骤:

  1. get the label element 获取标签元素
  2. get the li element contain the label 获取li元素包含标签
  3. get the index 获取索引

There is no click on label... it is empty. 没有点击标签...为空。 You can bind click on checkbox, the find the index of li and base on it color the same index of LI in your circle UL: 您可以绑定单击复选框,找到li的索引并基于它在您的圆UL中为LI的相同索引上色:

var sCircle = $('.steps ul li');

    $('input[type=radio]').on('click', function(){
       var liIndex=$(this).parents('li').index();
        if(liIndex>0){
            alert('error')
        }
        else{
            $('ul.clearfix>li:eq('+liIndex+')').css('background-color','red')
        }
    });

JSFIDDLE 的jsfiddle

 $(document).ready(function(){ var sCircle = $('.steps ul li'); $('.step label').on('click', function () { var currentLi = $(this).parents('li')[0]; currentLiIndex = -1; sCircle.each(function (index) { if (sCircle[index] == currentLi) { currentLiIndex = index; } }); console.log(currentLiIndex); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="clearfix"> <li> <div class="step-one circle"> <p>Step 1</p> </div> </li> <li> <div class="step-two circle"> <p>Step 2</p> </div> </li> <li> <div class="step-two circle"> <p>Step 3</p> </div> </li> </ul> <div class="stepinstallment steps"> <ul> <li id="step-one"> <h2>Choose your document</h2> <div class="step step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> <li id="step-two"> <h2>Choose your document</h2> <div class=" step step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> <li id="step-three"> <h2>Choose your document</h2> <div class="step step-content"> <label> <input type="radio" id="option-1" name="options" value="1" /> <span>ID card and household registration</span> </label> </div> </li> </ul> </div> 

This is what you need to do. 这是您需要做的。

$('.step-content label').on('click', function(){
  var clickedId = $(this).parents('li').index();
  if(clickedId == 0) {
    $('.circle').css('background-color','yellow');
  } else {
    alert('error');
  }
});

Working example in jsfiddle https://jsfiddle.net/1uxus551/ jsfiddle中的工作示例https://jsfiddle.net/1uxus551/

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

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