Javascript - 将变量添加到表单提交中

标签 javascript jquery html forms

我有一些 javascript 可以找到现有的 id 并将其替换为表单。

在该表单中,我希望能够将 javascript 变量添加到提交 url。我不确定这是否可行,或者我是否只是错误地实现了它。

$(document).ready(function () {
    $("#FB_cs_redirectTextBox")
    .replaceWith('<form class="cf" action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value" method="get">' +
                '<input type="search" placeholder="Search for stuff" name="search" class="searchbox-input" required="">' +
             '<select name="item-select" id="item-select" required="">' +
                '<option value="" selected="selected">Item type...</option>' +
                '<option value="level">item 1</option>' +
                '<option value="taste">item 2</option>' +
                '<option value="location">item 3</option>' +
                '<option value="month">item 4</option>' +
                '</select>' +   
             '<button type="submit" class="btn btn--green searchbox-submit">' +
                    'Search for items' +
                '</button>' +
            '</form>');


 });

所以我认为这是我的问题行:

action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value"

我也想知道这是否可能,我如何将 select 的值添加到 url 操作中。我会通过对所有选项应用相同的 id 然后在该 id 名称上调用 document.getElementById 来做到这一点吗?

非常感谢所有帮助!

最佳答案

给你的表单一个名字,然后:

document.form_name.action = 'http://test.com/s/search.html?profile=_default&collection=general' + document.getElementById(' + item-select + ').value;

确保在加载 DOM 后执行此操作,否则它将无法工作。

关于Javascript - 将变量添加到表单提交中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30918049/

相关文章:

javascript - 使用 componentWillRecieveProps 从 App.js 接收 Prop

c# - 根据 WebMethod 中提供的条件从 WebMethod 返回值

javascript - 选择 href 为 ="#"的标签并打开父 li

html - 模糊背景图像缩小它,如何在不缩小的情况下模糊?

javascript - 在 XMLHttpRequest 中使用相同的 session

c# - 使用 Jquery 从文本框向列表框添加项目

javascript - 如何执行被用户的广告拦截器拦截的外部 JS 文件

javascript - 如何定义相对于某个根目录的 typescript 导入路径(使用 webpack)?

javascript - Highcharts标签格式化程序来查询mysql?

javascript - 为什么我的链接在向导航添加负边距后不起作用?