[英]Jquery to select previous elements that arent siblings
I have DOM as below. 我有如下的DOM。
<div id="sortableWrapper">
<div class="questionWrapper">
<fieldset class="scheduler-border">
<div class="form-group">
<select class="ddlControlType">
<option>opt 1</option>
<option>opt 2</option>
</select>
</div>
</fieldset>
</div>
<div class="questionWrapper">
<fieldset class="scheduler-border">
<div class="form-group">
<select class="ddlControlType">
<option>opt 1</option>
<option>opt 2</option>
</select>
</div>
</fieldset>
</div>
<div class="questionWrapper">
<fieldset class="scheduler-border">
<div class="form-group">
<select class="ddlControlType">
<option>opt 1</option>
<option>opt 2</option>
</select>
</div>
</fieldset>
</div>
</div>
The div with class="questionWrapper" is dynamic and can be any number of them.. So lets say that i have 10 of them and im inside the 7th one. 带有class =“ questionWrapper”的div是动态的,可以是任意数量的。。所以可以说,我有10个,而im在第7个之内。 What i want is for the Previous 6 dropdowns to be selected.
我想要的是要选择“前6个”下拉列表。 I have an event that will grab the text of the dropdown from anyone the dropdowns the you select from.
我有一个活动,它将从您从中选择的任何下拉菜单的任何人那里获取下拉菜单的文本。
$(".ddlControlType").on("change", function () {
var b = $(this).parent().find(".ddlControlType option:selected").text()
});
I'm aware of the prev(), prevAll() methods, but those are for siblings of the same parent. 我知道prev()和prevAll()方法,但是这些方法用于同一父级的同级。 I dont think there is any way to relate these dropdowns as siblings, or am i wrong here.?
我不认为有任何办法可以将这些下拉菜单与同级联系起来,否则我在这里错了吗? Basically need to select the previous 6 dropdowns if im currently selecting on the 7th one.. Any other idea.?
如果我当前在第7个下拉菜单中选择,则基本上需要选择前6个下拉菜单。还有其他想法吗?
I dont think there is any way to relate these dropdowns as siblings, or am i wrong here.?
我不认为有任何办法可以将这些下拉菜单与同级联系起来,否则我在这里错了吗?
You need to consider the anchestor questionWrapper siblings, because each of them has as ddlControlType as a child. 你需要考虑anchestor questionWrapper兄弟姐妹,因为他们每个人都有为ddlControlType作为一个孩子。 Instead, parent() returns you a parent() and all these are not siblings.
相反, parent()返回一个parent(),而这些都不是同级的。
Hence, you need to get the .closest('.questionWrapper') and so all the previous items: 因此,您需要获取.closest('。questionWrapper')以及所有之前的项目:
$(this).closest('.questionWrapper').prevAll('.questionWrapper').find(".ddlControlType").val($(this).val())
$(".ddlControlType").on("change", function () { $(this).closest('.questionWrapper').prevAll('.questionWrapper').find(".ddlControlType").val($(this).val()) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="sortableWrapper"> <div class="questionWrapper"> <fieldset class="scheduler-border"> <div class="form-group"> <select class="ddlControlType"> <option>opt 1</option> <option>opt 2</option> </select> </div> </fieldset> </div> <div class="questionWrapper"> <fieldset class="scheduler-border"> <div class="form-group"> <select class="ddlControlType"> <option>opt 1</option> <option>opt 2</option> </select> </div> </fieldset> </div> <div class="questionWrapper"> <fieldset class="scheduler-border"> <div class="form-group"> <select class="ddlControlType"> <option>opt 1</option> <option>opt 2</option> </select> </div> </fieldset> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.