追加 HTML 数据 jQuery

Append HTML data jQuery

这次我在控制器中写了 html 当我选中一个复选框时,我在控制台中获取数据,但我想在表单中获取它。

我在 foreach 循环中有复选框

<input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required> {$student['name']}

当点击复选框时相应地加载数据

这是我的 html

    $str .= '<div class="form-group">
    <label class="control-label col-md-3">
    Student
    </label>
    <div class="col-md-4">
        <select id="students-clicked" class="form-control select2-multiple" multiple name="students[]">
            <option value=""></option>';
            foreach ($students as $students) {
                $str .= "<option value='{$student->id}'>{$student->name}</option>";
            }

$str .=         '</select>
    </div>
</div>';

Jquery 方法如

    $('body').on('click', "#chkbx", function() {
    var class = $('input:checkbox:checked').map(function() {
        return this.value;
    }).get();
    $.ajax({
            url: '/trigger/studentsData',
            type: 'POST',
            data: {'trigger_type': 'click' , ids: class},
            success: function(result) {

            }
        });
});

除了 jQuery 这次所有内容都写在控制器中我在控制台中获取 HTML 数据我如何将它附加到我的 html 表单中它也应该根据复选框选择更新

试试这个:-

<input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required/> {$student['name']}
<div id="appendata">
 //your ajax data
</div>

成功功能现已推出

success: function(result) {
    $("#appendata").html(result); // this will replace when you select the checkbox
         or
    $("#appendata").append(result); // this will append when you select again from checkbox
}

希望对您有所帮助

不要append() rather use html()用响应数据覆盖内容

success: function (result) {
    $("#container").html(result);
}

此外,如果您有多个复选框,请不要使用 id,而是使用 class 属性来遍历它们