简体   繁体   English

使用 jquery 从嵌套的 div 中获取 id

[英]get id from a nested div using jquery

I don't know how to put this.我不知道怎么写这个。 This has got a bit messy.这已经有点乱了。 I couldn't use the right words to ask the question.我不能用正确的词来问这个问题。 Here is my code:这是我的代码:

 <div id="addfieldcontainerdropdown"> <div id="addDropdownfield"> <div id="div0"> <select name="0" style="margin: 20px" id="0"> <option value="1">1</option> <option value="2">2</option> </select> <input style="margin: 7px" id="1" placeholder="Value" onkeyup="check_if_value_set(this); return false;" type="text"> <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><iclass="fa fa-times"></i></span> </div> <div id="div2"> <select name="2" style="margin: 20px" id="2"> <option value="1">1</option> <option value="2">2</option> </select> <input style="margin: 7px" id="3" placeholder="Value" onkeyup="check_if_value_set(this); return false;" type="text"> <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><i class="fa fa-times"></i></span> </div> <div id="div4"> <select name="4" style="margin: 20px" id="4"> <option value="1">1</option> <option value="2">2</option> </select> <input style="margin: 7px" id="5" placeholder="Value" onkeyup="check_if_value_set(this); return false;" type="text"> <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><i class="fa fa-times"></i></span> </div> </div> <a href="javascript:void(0)" onclick="show_new_text_field(this);event.preventDefault();return false;" style="cursor:pointer"> <span id="add_new_span" class="addnew" style="float: none;margin-left: 1%;"><i class="fa fa-plus fa-fw"></i>Add New Field</span> </a> </div>

You can see there is a function named "show_new_text_field(this)" at the bottom of the code.您可以看到代码底部有一个名为“show_new_text_field(this)”的函数。 Using this function, i need to get the id of say, div4 or select box of id 4. How can this be done ?使用此功能,我需要获取 div4 的 id 或 id 4 的选择框。如何做到这一点? I tried using prent() and closest().我尝试使用 prent() 和最接近的()。 Nothing worked.没有任何效果。 Any type of suggestions would be appreciated.任何类型的建议将不胜感激。 Thanks in advance !!提前致谢!!

Something like this?像这样的东西?

var lastDiv = $("[id^=div]").last(); //Get Last div whose ID begins with "div"
var lastDivId = lastDiv.attr('id'); //Get ID of that div
var firstSelect = lastDiv.find('select').first(); //Get the first 'select' of that div
var firstSelectId = firstSelect.attr('id'); //Get that select's ID

console.log("Last Div: " + lastDivId); // "Last Div: div4"
console.log("First Select in " + lastDivId + ": " + firstSelectId); //"First Select in div4: 4"

In the event your div won't always start with "div", you could just select the last child div of your parent ( addDropdownfield ) by doing this:如果您的 div 并不总是以“div”开头,您可以通过执行以下操作来选择父级的最后一个子divaddDropdownfield ):

var lastDiv = $("#addDropdownfield div").last(); //Get all children divs of addDropdownfield - from that list, just the last one

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

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