首页 > 解决方案 > 使用 ajax 从后端检索 HTML,然后使用 jquery 切片不起作用

问题描述

我有一个 JS 文件,它使用 ajax 从后端请求一个 HTML 文件,该文件作为 JSON 发送,然后解析为一个对象,如果我使用 jquery 显示 HTML 它工作正常,但如果我尝试切片 HTML 然后显示它工作。

$.ajax({
        url: "/QuizMaker/AnswerFormGrab/" + QuestionType,
        type: 'GET',
        dataType: 'html',
        contentType: false,
        processData: false,
        success: function (response) {
            var obj = JSON.parse(response);
           if (QuestionType === "MultipleChoiceQuestionRadio") {//QuestionType is correct
                $('#PreviewBody').html(obj.html);
                $('#PreviewBody').find("#Question").html(QuestionInfo[0].Question);
                var QuestionsLength = QuestionInfo.length -1;
                var htmlsplice2 = $(obj.html)//.Slice(1)
                for(i = 0; i < QuestionsLength; i++){//questionlength is 4
         $("#PreviewBody").find("#Question").after(htmlsplice2);
                }
            }
        }

    }
    );

如果我用 obj.html 替换变量 htmlsplice2,它是拼接的 html,obj.html 是通过 ajax 接收后解析的 html,它显示得很好,我也可以使用标准 html <p> Hello </p>,它也可以正常工作,但尝试拼接失败。

通过 ajax 检索到的 html 如下所示。

<p id="Question"></p>
<div class="row ModalRow">
    <div class="form-check">
        <input class="form-check-input AllAnswers" type="radio" name="exampleRadiosModal" id="ModalRadio" value="ModalRadio"/>
        <label class="form-check-label Answer" for="exampleRadios2">
        </label>
    </div>
</div>
<button class="btn btn-primary float-right">Submit</button>

我正在尝试对 p 标签和按钮标签进行切片,只留下行。

标签: javascripthtmljqueryajax

解决方案


推荐阅读