javascript - 使用 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 标签和按钮标签进行切片,只留下行。
解决方案
推荐阅读
- python - 是否可以通过使用具有有效属性名称的变量来调用类属性?
- javafx - JavaFX:无法动态将 ImageViews 添加到锚窗格
- vue.js - 在 Vue 中使用 SASS 中的变量来样式化组件和文件 scss
- python - 出现错误:模块“pdfkit”没有属性“配置”
- shell - 使用 jq 基于公共键值对合并两个 JSON 文件
- elasticsearch - Elasticsearch 到 BigQuery 管道部署在云数据融合实例上失败
- tibble - 如何在单列标题的开头插入一个值
- python - 错误:张量板 2.0.2 要求 setuptools>=41.0.0,但您将拥有不兼容的 setuptools 40.6.2
- google-bigquery - bigquery 命令按位置拆分列
- json - 将列表转换为字典 - 更新了我的问题