附加HTML数据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()宁愿使用 html()用响应数据覆盖内容

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

plus,如果您有一个以上的复选框,请不要使用id,而是使用class属性通过它们循环

最新更新