我目前正在为 Chrome 和 Fiferox 开发网络扩展。我正在使用 get 请求加载一些 Html 内容,并且需要在插入 DOM 之前更改 HTML 数据响应中的输入值之一。
以下是部分相关代码:
var url = browser.extension.getURL("resources/forms/form-a.html");
$.get(url, function(data){
$(data).find("input[id='name:front']").val("New Value");
//Here the console output is NOT "New Value"
console.log($(data).find("input[id='name:front']").val())
$("#form-names").replaceWith($(data));
});
我希望在替换 DOM 内容之前更改一些输入值。 一些想法?
提前致谢。
最佳答案
每次调用 $(data)
时,您都会解析响应并创建新的 HTML 元素 ( docs )。如果您保存了元素,更改了值,然后更新了 DOM,您会发现它工作正常。
var url = browser.extension.getURL("resources/forms/form-a.html");
$.get(url, function(data){
var newForm = $(data);
newForm.find("#name:front").val("New Value");
// Here the console output is should be "New Value"
console.log(newForm.find("#name:front").val())
$("#form-names").replaceWith(newForm);
});
关于javascript - 使用 JQuery 更改 XHR 请求中的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50145617/