Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用表单中的按钮将变量附加到包含其他表单变量的url_Javascript_Jquery_Html_Forms_Checkbox - Fatal编程技术网

Javascript 使用表单中的按钮将变量附加到包含其他表单变量的url

Javascript 使用表单中的按钮将变量附加到包含其他表单变量的url,javascript,jquery,html,forms,checkbox,Javascript,Jquery,Html,Forms,Checkbox,我有一个带有搜索栏和一些复选框的站点,这些将变量添加到url: term = "test" variable1=1 url/search?term=test&variable1=1 然后,搜索完成后,我在页面左侧有另一个表单,带有一些其他复选框和另一个搜索按钮。我希望在单击该按钮时,使用旧的搜索词和变量运行另一个搜索,但添加新的搜索词和变量 othervariable=1 url/search?term=test&variable1=1&othervariable=1

我有一个带有搜索栏和一些复选框的站点,这些将变量添加到url:

term = "test"
variable1=1
url/search?term=test&variable1=1
然后,搜索完成后,我在页面左侧有另一个表单,带有一些其他复选框和另一个搜索按钮。我希望在单击该按钮时,使用旧的搜索词和变量运行另一个搜索,但添加新的搜索词和变量

othervariable=1
url/search?term=test&variable1=1&othervariable=1
我尝试了其他线程的一些建议,但第二个搜索按钮似乎只将变量添加到url

url/othervariable=1
这对这个不起作用

第一种形式:


{%if termo%}
{%else%}
{%endif%}
递归的
宪法的
雷克拉玛曹
人身保护令
曼达多·塞古拉卡
刑事原稿
奥特罗斯
{%if总计%}

{%elif termo%}
Não foram encontradas decisões relacionadas ao termo{{{termo}。

{%endif%}
第二种形式:


表单将只提交属于它的字段(即
内部)。如果要在添加了新表单输入的情况下重复搜索,可以向侧表单(第二个)添加一个
,其中包含从get请求中获得的

因此,假设您从URL获得
termo_busca
,然后您可以添加:

<input type="hidden" value="{{termo}}" name="termo_busca" />
请注意,您将在GET参数中检查递归

另外,请注意,我删除了
{%else%}
,因为它没有做任何事情


更新:

就像我说的,
表单
只会提交它包含的值。如果您不想重复这些复选框,那么您需要一个javascript解决方案,在提交时将这些值“添加”到第二个表单中,或者镜像它们

以下是使用jQuery并在提交前添加它们的解决方案:

$('#diogo_form').on('submit', function(e) {
    e.preventDefault();

    let recursal_val = $('#search_form input[name="recursal"]').is(':checked') ? 1 : 0;
    $('#diogo_form').append(`<input type="hidden" name="recursal" value="${recursal_val}" />`);

    // ... do the same for all other values you want to copy
    // you could potentially also copy them all if you want them all
    // $('#search_form input[type="checkbox"]').each(function(i, e) {
    //   let name = $(e).attr('name');
    //   let checked = $(e).is(':checked');
    //   if(checked) {
    //     // add to diogo_form (see above)
    //   }
    // });

    $('#diogo_form').submit();
});
$('diogo_form')。关于('submit',函数(e){
e、 预防默认值();
让recursal_val=$('#search_form input[name=“recursal”]')。是否(':checked')?1:0;
$('#diogo_form')。追加(``);
//…对要复制的所有其他值执行相同的操作
//如果您想全部复制,也可以全部复制
//$(“#搜索_表单输入[type=“checkbox”]”)。每个(函数(i,e){
//让name=$(e).attr('name');
//let checked=$(e).is(':checked');
//如果(选中){
////添加到diogo_表单(见上文)
//   }
// });
$('diogo_form')。提交();
});

您可以将查询字符串中的值分配给表单中的html隐藏输入…这样它们将在第二次传递是的,
左侧的URL部分是位置,提交第二次get表单将忽略所有其他内容。嘿,这更有效!现在我只对第一个表单中的复选框有问题,比如“递归”。它适用于搜索项和第二个复选框,或者适用于搜索项和第一个复选框,但不同时适用于所有三个复选框……我不确定我是否理解,当您提交
diogo_表单
时,您没有获得原始(
search_表单
)请求的复选框值?您是否也为复选框添加了类似的
隐藏
输入?我尝试过,不确定是否正确,如果我在上面的值中添加了任何内容,它将保持锁定在该变量上,无论复选框是否选中,如果我将其保留为空,那么变量将不起作用,选中该框也不会做任何事情……这将创建第二个复选框,我只希望第一个表单上的复选框,然后我希望第二个表单上的按钮不会忽略第一个表单上复选框的选择。这样,第二个表单上的复选框就像第一个表单结果的过滤器一样工作。
$('#diogo_form').on('submit', function(e) {
    e.preventDefault();

    let recursal_val = $('#search_form input[name="recursal"]').is(':checked') ? 1 : 0;
    $('#diogo_form').append(`<input type="hidden" name="recursal" value="${recursal_val}" />`);

    // ... do the same for all other values you want to copy
    // you could potentially also copy them all if you want them all
    // $('#search_form input[type="checkbox"]').each(function(i, e) {
    //   let name = $(e).attr('name');
    //   let checked = $(e).is(':checked');
    //   if(checked) {
    //     // add to diogo_form (see above)
    //   }
    // });

    $('#diogo_form').submit();
});