如何重用 ajax 表单加载函数打开多个选项卡,每个选项卡具有不同的参数
How to reuse an ajax form-loading function to open up several tabs with different parameters each?
我在不同的网页上有3个表单,每个表单都有一个提交按钮。 单击单个页面上的"提交",将打开对未知来源process_form
函数(请参阅下面的函数)的 AJAX 调用,该函数将为您加载表单。 本质上,单击"提交"后,该函数将表单值组合到请求中,并通过 AJAX 请求加载子页面。
3 的每种形式在其核心上都是相同的,但略有变化和选择。
我想做的是将 3 个表单合并为一个表单,然后让我单击"提交"打开 3 个新的浏览器选项卡,每个特定表单选项一个。
所以我正在寻找一种方法来重用这个process_form
函数(可取的),打开窗口,或者修改它(不希望它被许多其他文件使用)来实现我的目标。
请参阅下面的片段,了解它如何在我这边工作。 我正在寻找的答案是使用一个表单来打开 3 个原始表单,但每个表单都在一个新的选项卡窗口中。
//abridged version of the library function I use for AJAX
function process_form(form, url, redir, response, method, delay) {
var formObj = document.getElementById(form);
var post_str = "";
if (method == "POST") {
for (var i = 0; i < formObj.length; i++) {
if (formObj.elements[i].tagName == "INPUT") {
if (formObj.elements[i].type == "text") {
post_str += escape(formObj.elements[i].name) + "=" + encodeURIComponent(formObj.elements[i].value) + "&";
}
}
}
}
//here this will be a load_page() call
//but replaced here with alert to show functionality
alert("Load Form with [" + post_str + "]");
}
<form id="a">
<input name="opt_a" type="text" value='4' />
<input type="button" onclick="process_form('a', '', '', '', 'POST', '')" value="SelectA" id="selButton" name="selButton" />
</form>
<form id="b">
<input name="opt_b" type="text" value='5' />
<input type="button" onclick="process_form('b', '', '', '', 'POST', '')" value="SelectB" id="selButton" name="selButton" />
</form>
<form id="c">
<input name="opt_c" type="text" value='6' />
<input type="button" onclick="process_form('c', '', '', '', 'POST', '')" value="SelectC" id="selButton" name="selButton" />
</form>
合并的表单将如下所示(未完成)
<form id="a_b_c">
<input name="opt_a" type="text" value='4' />
<input name="opt_b" type="text" value='5' />
<input name="opt_c" type="text" value='6' />
<!-- submit functionality opening up separate tabs is what I seek -->
</form>
您的<form>
标签没有属性,因此,如果您的实际代码执行相同的操作,它将通过 GET 发送表单值。
JavaScript,您可以获取表单元素的值,使用URL中包含的表单中的值构建适当的URL(例如。 mypage.php?input1=value1&input2=value2
),然后使用window.open()
打开三个新选项卡,每个选项卡中都有相应的 URL。
例:
function process_form() {
var value1 = document.getElementById('value1').value;
var value2 = document.getElementById('value2').value;
var value3 = document.getElementById('value3').value;
window.open('mypage.php?value1=' + value1);
window.open('mypage.php?value2=' + value2);
window.open('mypage.php?value3=' + value3);
}
<form>
<input type="text" name="value1" id="value1" />
<input type="button" onclick="process_form()" value="SelectA" id="selButton" name="selButton" />
</form>
<form>
<input type="text" name="value2" id="value2" />
<input type="button" onclick="process_form()" value="SelectB" id="selButton" name="selButton" />
</form>
<form>
<input type="text" name="value3" id="value3" />
<input type="button" onclick="process_form()" value="SelectC" id="selButton" name="selButton" />
</form>
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- ExternalInterface为我提供了在使用多个参数时停止脚本的选项.为什么?
- 将选项框的值和键作为参数发送
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 服务器不读取科尔多瓦文件传输选项参数
- 根据 GET 参数选择选项
- 通过参数设置html组合框的选定选项
- 当表单控件选项具有多个参数时,如何在组合框中填充所选项目
- 何时使用which-多个方法、多个参数或一个选项参数
- 本地页面是否可以接受文本值,并以值作为参数在Internet URL上打开新选项卡
- 如何重用 ajax 表单加载函数打开多个选项卡,每个选项卡具有不同的参数
- ES6 默认选项参数
- 如何将下拉列表中所选项的值作为参数传递给控制器(Google 图表)
- 接受选项对象作为参数的简洁方法,回退到省略属性的默认值
- Jquery Ajax 没有拾取选项参数中指定的函数回调
- 选择 2 添加其他参数以选择选项
- Sequelize更新不再工作:;传递给update的选项参数中缺少where属性;
- 如何在ES6中使用所有默认值来销毁选项参数
- Handlebars使用选项参数编译函数
- 选项参数在回调参数之前