简体   繁体   English

如何通过jQuery或JavaScript将所选参数传递给表单操作URL

[英]How to pass selected parameters to a form action url via jQuery or JavaScript

I've been searching a lot here and in Google about how to pass selected parameters to the action from URL as I prefer. 我一直在这里和谷歌搜索关于如何根据我的喜好将所选参数传递给URL中的操作。 But I don't have any idea how to do it. 但我不知道该怎么做。 The code that I created doesn't meet the purpose since my level is simple in Javascript! 我创建的代码不符合目的,因为我的级别在Javascript中很简单!

My code: 我的代码:

 $('#search-filter').on('submit', function() { var $this = $(this), getValue = $this.attr('action') + $('.uk-radio').val(); $this.attr('action', getValue); $this.submit(); }); 
 #search-filter { padding: 40px; } #search-filter .uk-form-icon { padding: 0 10px; right: 40px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <form id="search-filter" class="uk-margin-medium" action="https://example.com/search" target="_top"> <h4 class="search-title">Search Filter</h4> <div class="search-input"> <div class="uk-margin uk-grid-small uk-child-width-auto uk-grid"> <div class="filters-title">Level:</div> <label><input class="uk-radio" type="radio" name="level" value="beginner"> Beginner</label> <label><input class="uk-radio" type="radio" name="level" value="intermediate"> Intermediate</label> <label><input class="uk-radio" type="radio" name="level" value="advanced"> Advanced</label> </div> <div class="uk-margin uk-grid-small uk-child-width-auto uk-grid"> <div class="filters-title">Type:</div> <label><input class="uk-radio" type="radio" name="type" value="theorie"> Theorie</label> <label><input class="uk-radio" type="radio" name="type" value="practical"> Practical</label> </div> <div class="uk-margin"> <label class="uk-form-label" for="form-horizontal-select">Select a category:</label> <div class="uk-form-controls"> <select class="uk-select" id="form-horizontal-select"> <option value="category1">Category 01</option> <option value="category2">Category 02</option> <option value="category3">Category 03</option> <option value="category4">Category 04</option> </select> </div> </div> <button class="uk-button uk-button-primary">Filter</button> </div> </form> 

The output I have got: 我得到的输出:

https://example.com/searchbeginnerbeginnerbeginner?level=beginner&type=practical

The output I really need: 我真正需要的输出:

https://example.com/search?label=beginner+practical+category01&max-results=4

As you see there are three parameters within + sing after a label= then a parameter max-results after & sign. 如你所见,在标签= +之后有三个参数,然后是符号后面的参数max-results

There is any method to get such as this link above? 有什么方法可以获得如上面这个链接?

Thanks in advance! 提前致谢!

You can do the following for your JavaScript 您可以为JavaScript执行以下操作

$('#search-filter').on('submit', function(e) {
    e.preventDefault();
    var $this = $(this);
    var queryParams = 'label=' + $('input[name="level"]').val() 
                               + '+' + $('input[name="type"]').val()
                               + '+' + $('#form-horizontal-select').val()
                     + '&max-results=4';
    window.open('https://example.com/search?'+queryParams, "_blank");
});

If you want the category values to be category 0* , then you must also update the option values to 如果希望类别值为类别0 * ,则还必须将选项值更新为

<select class="uk-select" id="form-horizontal-select">
    <option value="category01">Category 01</option>
    <option value="category02">Category 02</option>
    <option value="category03">Category 03</option>
    <option value="category04">Category 04</option>
</select>

To get select option value you can use the following coding: 要获得选择选项值,您可以使用以下编码:

var selectBox=document.getElementById("form-horizontal-select");
getSelect=selectBox.options[selectBox.selectedIndex].value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MVC中的JavaScript中传递url.action中的多个参数? - How to pass multiple parameters in url.action in JavaScript in MVC? 如何使用@ url.Action使用JavaScript传递带有值的参数 - How to pass parameters with value using @url.Action using javascript 如何通过JavaScript将动态URL添加到WordPress中的表单操作? - How to add a dynamic URL to a form action in WordPress via JavaScript? Django / Jquery / Javascript - 如何使用url参数预填充表单(自动填充表单) - Django / Jquery / Javascript - How to prepopulate form with url parameters (autofill the form) 如何在jQuery中传递URL参数 - How to pass url parameters in jQuery 如何在Url.Action()中传递多个参数 - How to pass multiple parameters in Url.Action() 需要从表单中获取Selected Option值,然后通过jquery / javascript返回 - Need to grab Selected Option value from a form and pass back via jquery/javascript 如何传递从 django 表单操作 url 的下拉列表中选择的项目的 id? - how can I pass the id of the item selected from the dropdown in django form action url? 通过javaScript传递2个参数 - pass 2 parameters via javaScript 如何在 vuejs 中以表单动作 url 传递变量 - How to pass a variable in form action url in vuejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM