gpt4 book ai didi

javascript - jQuery/JavaScript : Capture submit event from form in iframe?

转载 作者:搜寻专家 更新时间:2023-11-01 05:05:49 24 4
gpt4 key购买 nike

假设我有一个带有 iframe 的网页,其中包含一个表单。提交表单后,我想在隐藏字段中设置一个值。提交表单时如何在主页触发事件?

这是我正在尝试做的事情的简化版本:

  <!DOCTYPE html>
<html>
<head>
<title>TEST IFRAME UPLOAD</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
// Pseudo code for what I want to do
// (this of course doesn't work because target is inside iframe:
jQuery(document).ready(function() {
$("#file_upload_form").submit(function() {
$("input[name='fileuploaded']").val(1);
return true;
});
});
</script>
</head>

<body>

<form id="mainform" action="processform" method="post">
<input type="hidden" name="fileuploaded" value="">
<!-- more form fields here -->
</form>

<iframe id="upload-iframe">
<form enctype="multipart/form-data" id="file_upload_form" action="processfileupload" method="post">
Select an image to upload:<br/>
<input type="file" name="file">
<input type="submit" name="submit" class="submit" value="Upload">
</form>
</iframe>

</body>
</html>

最佳答案

试试这个,

$('#upload-iframe').load(function() {
$(this).contents().find('#file_upload_form').submit(function() {
$("input[name='fileuploaded']").val("some value"); // updated
return true; //return false prevents submit
});
});

引用自 How can i bind an event to an element inside a local iframe?

可能与Selecting a form which is in an iframe using jQuery 重复

关于javascript - jQuery/JavaScript : Capture submit event from form in iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17227641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com