使用 JQuery 更改 XHR 请求中的输入值

Change value of input in XHR request with JQuery

我目前正在为 Chrome 和 Fiferox 开发一个网络扩展。我正在使用获取请求加载一些 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);
});