[英]better options for writing this jquery
I have a html file like the following one, 我有一个类似以下内容的html文件,
<i class=‘icon-calendar icon-large' data-placement='top' data-toggle='popover' type='button'>
<div class='head hide'>Calendar</div>
<div class='content hide'>
<form action="#" data-remote="true" id="calendar_form" method="post">
<div style="...">
<input type="hidden" />
<input type="hidden" />
</div>
<table border='0px'>
<tbody>
<tr>
<td colspan='2'>
<button class='btn btn-default btn-block' id='submit' type='submit'>Convert</button>
</td>
</tbody>
</table>
</form>
</div>
</i>
<li class="..." id="calendar_example"><label class=" label" for="...">...</label>
<input class="..." id="calendar_example_1" name="[calendar_example][0][calendar_example_1][0]" type="text" />
I'm trying to use Jquery to write the function as when click button where id is submit, i can pass value from the popover to the field with id calendar_example_1, my code, the working one to find the field, is 我正在尝试使用Jquery来编写函数,就像单击提交ID的单击按钮时一样,我可以将值从弹出窗口传递给ID为calendar_example_1的字段,我的代码是找到该字段的有效代码,
$(function(){
$('body').on('click','#submit', function(){
var calendar_name = $(this).parent().parent().parent().parent().parent().parent().siblings().find('#calendar_example_1').attr('name');
return false;
});
});
by using .parent() multiple times, i don't think this is a good way to write this code. 通过多次使用.parent(),我认为这不是编写此代码的好方法。 As i'm new to jquery, i can't find any better solution yet that works the way i want. 由于我是jquery的新手,因此找不到任何更好的解决方案,可以按我想要的方式工作。 Anyone can give me some suggestion? 有人可以给我一些建议吗?
EDIT: the outside form is nested. 编辑:外部窗体是嵌套的。
您可以这样做:
var calendar_name = $(this).closest("i.icon-calendar").siblings("li#calendar_example").find('#calendar_example_1').attr('name');
You can use 您可以使用
$('#submit').click(function(){
var calendar_name = $('#calendar_example_1').attr('name');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.