简体   繁体   English

如何通过ID查找最接近的div,然后查找该div中所有表单的ID

[英]How to find the closest div by ID and then find the ID's of all forms inside that div

I am looking to to find the closest div with an class of 'form_wrap' to the button when clicked and then find the ID's of all forms inside that div. 我希望找到被单击时与按钮最接近的类,其类别为“ form_wrap”,然后查找该div中所有表单的ID。

The forms sit as part of two Laravel foreach loops and have been given individual dynamic ID numbers. 这些表单是两个Laravel foreach循环的一部分,并已获得单独的动态ID号。 These ID numbers need to be collected and then submitted. 这些ID号需要收集然后提交。

<div class="form_wrap">  
    @foreach ($workouts as $workout)
         <?php $formid="form_".$x."_".$f;?>
         {!! Form::open(array('route' => 'workoutshared.store', 'method' => 'POST', 'ID' => $formid)) !!}
         {{ csrf_field() }}
         {{ Form::hidden('user_id', Auth::user()->id)}}
         {{ Form::hidden('date', $entry)}}
         {{ Form::hidden('weight', $workout->weight)}}
         {{ Form::hidden('exercise', $workout->exercise)}}
         {{ Form::hidden('reps', $workout->reps)}}
         {{ Form::hidden('sets', $workout->sets)}}
         <button id="submit" class="btn btn-default">Share</button>               
         {{ Form::checkbox('share', null, array('class' => 'form-control', 'required' => ''))}}
         {!! Form::close() !!}     
         <tr>
               <th>{{$workout->exercise}}</th>
               <td>{{$workout->weight}}</td>
               <td>{{$workout->reps}}</td>
               <td>{{$workout->sets}}</td>
         </tr>
    <?php $f++; endforeach;?>
 </div>

I think you want $( "[id^='form_']" ) which finds any ID that starts with "form_" although using a class is a better option when trying to find more than one element on a page. 我认为您想要$( "[id^='form_']" )来查找任何以“ form_”开头的ID,尽管在尝试在页面上查找多个元素时使用类是一个更好的选择。

var formWrap = $( "#SomeSpecificID" ).parent();
var formsInsideThatID = formWrap.children().attr('id');

There are many details you've left out, so the answers may need to be modified to match. 您遗漏了许多细节,因此答案可能需要修改以匹配。

For example - do you want to allow the form submission? 例如-您要允许表单提交吗? Intercept it? 拦截吗? Or do you care? 还是在乎?

Another point is - what do you want to do with the ID's once you have them? 另一点是-一旦拥有ID,您想如何处理?

This code will do what you've asked. 该代码将完成您所要求的。 It uses event delegation, so you should be able to load it in your <head> , and it will still work as requested. 它使用事件委托,因此您应该可以将其加载到<head> ,并且仍然可以按要求工作。

// Bind to the click event of all form buttons
$(document).on('click', 'form button', function() {
    // Find the closest div
    var wrap = $(this).closest('div.form_wrap');

    // Then, find all form ID's inside of the div:
    wrap.find('form').each(function() {
        var id = $(this).prop('id');
    });
});

What you do with those ID's is up to you, but there's several options, depending on your needs. 您可以根据自己的意愿来处理这些ID,但是有多种选择,具体取决于您的需求。

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

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