【问题标题】:Submit multiple forms by their ID using AJAX使用 AJAX 按 ID 提交多个表单
【发布时间】:2016-10-02 17:50:10
【问题描述】:

我希望使用 AJAX 通过他们的 ID 提交多个表单,ID 是使用下面的 jQuery 找到和生成的。当然,每个表单都有单独的唯一 ID,目前由变量 i 创建。

     <script>
$(document).on('click', '#button', function() {

    var wrap = $(this).closest('div.form_wrap');

    wrap.find('form').each(function() {
        var id = $(this).prop('id');
        var i = $('#'+id);
        console.log(i);
    });

});
</script> 

HTML

<?php $f = 0;?>  
@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)}}              
    {{ Form::checkbox('share', 1, array('class' => 'form-control'))}}
{!! Form::close() !!}     
    <tr>
        <th>{{$workout->exercise}}</th>
        <td>{{$workout->weight}}</td>
        <td>{{$workout->reps}}</td>
        <td>{{$workout->sets}}</td>
    </tr>

<?php $f++; endforeach;?>

更新

<script>
$(document).on('click', '#button', function() {

    var wrap = $(this).closest('div.form_wrap');

    wrap.find('form').each(function() {
        var id = $(this).prop('id');
        var i = $('#'+id);
        console.log(i);
        $(i).submit(function() {
        var that = $(this),
            url = that.attr('action'),
            type = that.attr('method'),
            data = {};

        that.find('[name]').each(function(index, value) {
            var that = $(this),
                name = that.attr('name'),
                value = that.val();

            data[name] = value;
        });

        $.ajax({
            url: url,
            type: type,
            data: data,
            success: function(response) {
                console.log(response);
            }
        });

        return false;
    });
    });

});

</script>

【问题讨论】:

  • 你的 HTML 在哪里?
  • 具体问题和疑问是什么?
  • 请注意&lt;form&gt; 不能是&lt;tr&gt; 的兄弟...这是无效的html
  • 我对 AJAX 的经验很少,希望使用 i 的 jQuery 变量生成的 ID 提交表单。表格的数量是可变的。

标签: javascript php jquery ajax forms


【解决方案1】:

您可以使用以下方式提交所有表单:

wrap.find('form').each(function() {
var id = $(this).attr('id');
var i = $('#'+id);
  $(i).submit(function() {
        var that = $(this),
            url = that.attr('action'),
            type = that.attr('method'),
            data = {};

        that.find('[name]').each(function(index, value) {
            var that = $(this),
                name = that.attr('name'),
                value = that.val();

            data[name] = value;
        });

        $.ajax({
            url: url,
            type: type,
            data: data,
            success: function(response) {
                console.log(response);
            }
        });

        return false;
    });
});

显然使用 this 会获取表单中已经使用的数据,并使用当前方法将其提交给当前操作。

因此,如果您使用 GET 和 POST 以及不同的提交位置(操作),这基本上是可行的。

编辑:我还根据 jquery 文档将 .prop() 更改为 .attr():http://api.jquery.com/attr/ .prop() 在这种情况下不是正确用法

【讨论】:

  • 感谢您的回复,但它失败了。你能看看为什么会这样吗?
  • 为什么会失败?怎么了?是否有任何控制台错误?
  • 控制台中什么都没有。我认为 jQuery 一开始可能会失败。根据$(document).on('click', '#button', function() { var wrap = $(this).closest('div.form_wrap'); wrap.find('form').each(function() { var id = $(this).attr('id'); var i = $('#'+id); console.log(i); });,没有任何文档或单击#button 我已尝试调整您的代码,但没有运气。
  • 如果您在 onClick 中使用它,我会将 $(i).on('submit', function() { 更改为 $(i).submit(function() {
  • 仍然失败是通过变量 i 现在正确地拉动。可能是 URL 或类型的问题。我已经发布了更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多