[英]Using $(this) scope to find parent previous element
I have a HTML that looks like: I am trying to get current parent closest object value. 我有一个HTML看起来像:我正在尝试获取当前父对象最近的对象值。 In this case I'm trying to get values of owner_address, owner_name. 在这种情况下,我尝试获取owner_address,owner_name的值。
<div class = "form-group row">
<div class="col-xs-6 form-group">
<input id="owner_name" type="text></input>
</div>
<div class="col-xs-6 form-group">
<input id="owner_address" type="text></input>
</div>
</div>
<div class="form-group row">
<div class="col-xs-6 form-group">
<input onclick="runFunction($(this))" id="update" type="text></input>
</div>
</div>
JS part where I try to get values. 我尝试获取值的JS部分。
<script>
function runFunction(thisObj){
var owner_name = thisObj.parent().closest('#owner_name').val()
#Gives me undefined.
}
</script>
I already tried selecting it using ID; 我已经尝试过使用ID选择它; it doesn't work in my case because the data are being rendered inside a modal; 在我的情况下,它不起作用,因为数据是在模态内部呈现的; where each modals are unique. 每个模态都是唯一的 I want to get specific owner name inside my each unique modal; 我想在每个唯一模式中获取特定的所有者名称; that is why I thought using $(this) would be convenient. 这就是为什么我认为使用$(this)会很方便的原因。
尝试这个
var owner_name = thisObj.parent().parent().closest('.form-group').find('#owner_name).val()
try this demo: https://jsfiddle.net/xianshenglu/4nt42cso/4/ 试试这个演示: https : //jsfiddle.net/xianshenglu/4nt42cso/4/
i change the js,if you use id selector you don't have to use other selectors. 我更改了js,如果您使用id选择器,则不必使用其他选择器。
function runFunction(thisObj){
var owner_name = $('#owner_name').val()
alert(owner_name);
}
also change your html,just correct the syntax error.for example,there should not be code like this:</input>,,,, 还要更改您的html,只需更正语法错误即可。例如,不应使用以下代码:</ input> 、、、
<div class="form-group row">
<div class="col-xs-6 form-group">
<input id="owner_name" type="text" />
</div>
<div class=" col-xs-6 form-group ">
<input id="owner_address " type="text" />
</div>
</div>
<div class="form-group row">
<div class="col-xs-6 form-group">
<input onclick="runFunction($(this))" id="update" type="text" />
</div>
</div>
Put this in your fucntion and check it return desired value or not. 将其放在您的功能中,并检查其是否返回所需的值。
thisObj.parent().parent().parent().find( "#owner_name" ).val(); thisObj.parent()。parent()。parent()。find(“ #owner_name”).val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.