简体   繁体   English

如何获取父类的最接近表单元素。 jQuery的

[英]How to get closest form element of parent class. jQuery

I am trying to add vote up buttons to my blog comments. 我试图在我的博客评论中添加投票按钮。 I am looping through each comment like this: 我正在这样循环浏览每个评论:

.row
    for comment in Comments
        .row
            p #{comment}
            .votecell(style='width:30px;float:left;margin-left: 15px;')
                .voteUp(id='#{comment.id}', style='margin-bottom: -17px;') ▲
                            form.form-horizontal(id='voteUp', enctype="multipart/form-data", method='POST')
                                input(type='hidden', name='_csrf', value=_csrf)
                                input(type='hidden', name='commentID', value='#{comment.id}')
                                <br>

Then using jQuery I am trying to submit the form that is a child of the button that was clicked. 然后使用jQuery,我试图提交作为被单击按钮的子级的表单。

$(".voteUp").click(function () {
    var form = $(this).closest(".form-horizontal")
    $(function(){
        $.ajax({
            url: '/voteUp', //the URL to your node.js server that has data
            type: 'POST',
            data:  form.serialize()
        }).done(function(data){

        }); 
    });
});

The problem is it always submits the form from the first comment on the page no matter which voteUp button is clicked. 问题在于,无论单击哪个votUp按钮,它始终从页面上的第一条注释提交表单。 I know this is because in jQuery $(this) == this[0], but I am unsure what other options are available to reference the clicked object. 我知道这是因为在jQuery $(this)== this [0]中,但是我不确定还有哪些其他选项可用来引用单击的对象。 Anyone know how I can grab the child form of the clicked button? 有人知道我该如何获取单击按钮的子窗体?

Here is what the html output looks like: 这是html输出的样子:

<div id="5463ce6bd6e5e5d403e80b3f" style="margin-bottom: -17px;" class="voteUp">
    ▲
    <form enctype="multipart/form-data" method="POST" class="form-horizontal">
        <input type="hidden" name="_csrf" value="4JbLbnEcRvq7ZuAhpZCXavMq7JW/vnIZlxW/o=">
        <input type="hidden" name="commentID" value="5463ce6bd6e5e5d403e80b3f">
    </form>
</div>

You could try using the .next() method. 您可以尝试使用.next()方法。

$('.voteUp').next('.form-horizontal')

That would select the next element with the form-horizontal class and only that. 这将选择带有水平窗体类的下一个元素,仅此而已。

Here's the documentation - see if it's what you need. 这里是文档-看看是否正是您所需要的。 http://api.jquery.com/next/ http://api.jquery.com/next/

Try using children instead of closest. 尝试使用孩子代替最近的孩子。 http://jsfiddle.net/hLraqdq1/ http://jsfiddle.net/hLraqdq1/

$(".voteUp").click(function () {
    var form = $(this).children(".form-horizontal");
    $(function(){
        $.ajax({
            url: '/voteUp', //the URL to your node.js server that has data
            type: 'POST',
            data:  form.serialize()
        }).done(function(data){

        }); 
    });
});

So the main issue is you are traversing in the wrong direction within the DOM tree. 因此,主要问题是您在DOM树中的移动方向错误。

closest() starts with current selector and looks up the tree for ancestors but you are wanting to look for descendents. closest()从当前选择器开始,并在树中查找祖先,但您想查找后代。

Just use find() instead of closest() 只需使用find()而不是closest()

References: 参考文献:

closest() API docs 最近的API文档

find() API docs find()API文档

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

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