在表单($ this)

时间:2019-02-03 15:03:08

标签: jquery

我正在尝试在提交的表单中按名称选择输入元素。

jQuery('.formBet').on("submit", function(e) {
    e.preventDefault();
    var $form = $(this);
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        }
    });
    jQuery.ajax({
        url: $form.attr('action'),
        method: 'post',
        data: {
            match_id: $( this ).children( "input[name='match_id']" ).val(),
            home_score: $( this ).children( "input[name='home_score']" ).val(),
            away_score: $( this ).children( "input[name='away_score']" ).val(),
        },
        success: function(result){
            console.log(result);
        },
        error:function(result) {
            console.log(result);
        }
    });
});

我有许多同一个班级的表格,所以必须是“这个”。

我得到不确定的值。

1 个答案:

答案 0 :(得分:1)

使用由.find()插入的.children()

match_id: $( this ).find( "input[name='match_id']" ).val(),

.children()仅在DOM树下移动一个级别,而 .find()可以遍历多个级别以选择后代元素。