Javascript 使用jQuery将带有HTML帖子的iframe作为目标
如果我使用jQuery或JavaScript发布文章,如何使其以iframe而不是当前页面为目标Javascript 使用jQuery将带有HTML帖子的iframe作为目标,javascript,jquery,ajax,iframe,Javascript,Jquery,Ajax,Iframe,如果我使用jQuery或JavaScript发布文章,如何使其以iframe而不是当前页面为目标 jQuery.post( url, [data], [callback], [type] ) 这是jQuery帖子的格式,似乎没有任何地方可以像标记中那样指定目标 有什么想法吗?也许您没有抓住AJAX请求的要点-为什么您要尝试指定异步请求的“目标”?这是毫无意义的,因为AJAX的整个要点是来自服务器的请求被发送回Javascript,没有页面重新加载或H
jQuery.post(
url,
[data],
[callback],
[type]
)
这是jQuery帖子的格式,似乎没有任何地方可以像
标记中那样指定目标
有什么想法吗?也许您没有抓住AJAX请求的要点-为什么您要尝试指定异步请求的“目标”?这是毫无意义的,因为AJAX的整个要点是来自服务器的请求被发送回Javascript,没有页面重新加载或HTML目的地 如果希望将请求的结果加载到iframe,可以执行以下操作:
$.post('myurl', function(data) {
$('#myframe').html(data);
}, 'html');
你错过了AJAX的要点。如果您想将某个内容发布到iframe,可以通过一个简单的表单,通过JavaScript发布,或者通过通常的方式:提交按钮来完成。您可以通过一个常规表单:
<form action="targetpage.php" method="post" target="iframename" id="formid">
<input type="hidden" name="foo" value="bar" />
</form>
通过动态创建表单并将其附加到主体中,可以解决表单中不允许包含表单的问题。所以你会这样做:
$().ready(function() {
$('body').append('<form
action="url"
method="post"
target="iframename"
id="myspecialform">
<input type="hidden" name="parameter" value="value" />
</form>');
});
$().ready(函数()){
$('body')。追加('
');
});
这将在包含页面上所有其他内容的主表单之外创建iframe更新表单。然后按照Josh的建议调用它:
$(“#myspecialform”).submit()代码>当我需要通过表单(理想情况下使用AJAX/jQuery)将数据提交到远程页面时,我做了以下工作来解决在asp.net页面的表单中包含表单的问题
我创建了变量来捕获asp.net表单名称、目标、,
行动、方法等
我把表格上的信息保存在
然后使用jQuery更改表单本身
我需要它做什么
然后我通过AJAX发布表单(因为我需要一个表单来动态发布到一个单独的页面,所以另一个
页面可以获取信息)
最后,我更改了asp.net表单
这样页面的其余部分就可以正常工作了
这似乎解决了我对类似解决方案的需求。我知道这个问题很老了,但下面是我如何在ASP.Net(C#)上使用jQuery实现的
//Create the form in a jQuery object
$("<form action='/FormPostTo.aspx' method='post' target='nameofframe'></form>")
//Get the value from the asp textbox with the ID txtBox1 and POST it as b1
.append($("<input type='hidden' name='b1' />").attr('value',$('#<%= txtBox1.ClientID %>').val()))
//Get the value from the asp textbox with the ID txtBox2 and POST it as b2
.append($("<input type='hidden' name='b2' />").attr('value',$('#<%= txtBox2.ClientID %>').val()))
//Add the form to the body tag
.appendTo('body')
//Submit the form which posts the variables into the iframe
.submit()
//Remove the form from the DOM (so subsequent requests won't keep expanding the DOM)
.remove();
//在jQuery对象中创建表单
$("")
//从ID为txtBox1的asp文本框中获取值,并将其发布为b1
.append($(“”).attr('value',$('#')).val())
//从ID为txtBox2的asp文本框中获取值,并将其发布为b2
.append($(“”).attr('value',$('#')).val())
//将表单添加到body标记
.appendTo('正文')
//提交将变量发布到iframe的表单
.提交()
//从DOM中删除表单(这样后续请求就不会继续扩展DOM)
.remove();
请注意:我这样做了输入标记,而不是将它们串联起来,以防文本框的值中有引号(”
)。如果你把它连在一起,它会弄乱HTML,并且不能正确解析
此外,这会在表单被使用后将其从DOM中删除,因此如果多次发布到iFrame,则不会用表单元素填充DOM
您可以做一个小小的更改,即如果表单元素不存在,则创建表单元素,如果表单元素已经存在,则仅通过ID引用它并重用它。以下是我在javascript中使用纯html实现的方法:
var form=$("<form/>").attr({
method: "post",
action: "iframe.php",
target: "list_frame"
});
form.append($("<input/>").attr({name:"field1",value:0}));
form.append($("<input/>").attr({name:"field2",value:1}));
form.append($("<input/>").attr({name:"field3",value:2}));
form.append($("<input/>").attr({name:"field4",value:3}));
form.append($("<input/>").attr({name:"field5",value:4}));
$("body").append(form);
form.submit();
var form=$(“”).attr({
方法:“张贴”,
动作:“iframe.php”,
目标:“列表框”
});
append($(“”).attr({name:“field1”,value:0}));
append($(“”).attr({name:“field2”,value:1}));
append($(“”).attr({name:“field3”,value:2}));
append($(“”).attr({name:“field4”,value:3}));
append($(“”).attr({name:“field5”,value:4}));
$(“正文”)。附加(表格);
表单提交();
不能对表单执行此操作,我在.net上,您不能在表单中包含表单。您可以使用主表单。使用jquery临时更改action、target等的值,然后在ASP.NET MVC允许多个表单后恢复这些值。另外,我认为如果您使用jQuery动态呈现表单,然后提交表单,那么ASP.NET将不会阻塞。这个过程可以在.html页面上完成吗?Matt您可以通过iframe将html表单调用到.NET页面中。我并不真正关心ajax。我只想从外部将帖子提交到iframe,而用户无需单击提交按钮。我无法使用src将它们传递到iframe中,因为它不需要querystring参数。该标记允许我完全按照自己的意愿进行操作,但是我在.net上,因此所有内容都包含在表单标记中,并且我无法使用target属性为自己创建新的表单标记。由于某种原因,当我使用您提供的代码时,不会调用任何东西。我尝试替换#(“#myframe”).html(数据);警惕(“成功”);如果你不想使用'target'属性,表单可以直接进入iframe:$('iframe#list_frame').contents().find('body').append(表单);在FF和Chrome为我工作
var form=$("<form/>").attr({
method: "post",
action: "iframe.php",
target: "list_frame"
});
form.append($("<input/>").attr({name:"field1",value:0}));
form.append($("<input/>").attr({name:"field2",value:1}));
form.append($("<input/>").attr({name:"field3",value:2}));
form.append($("<input/>").attr({name:"field4",value:3}));
form.append($("<input/>").attr({name:"field5",value:4}));
$("body").append(form);
form.submit();