从表单提交自定义查询字符串

Submitting a custom query string from form

本文关键字:字符串 查询 自定义 表单提交      更新时间:2023-09-26

我有一个表单,当我提交它的时候,它会把我带到网址www.domain.com/search/?maxprice=100000,但是我想让它把我带到一个自定义的url,例如www.domain.com/search/maxrprice_10000000/

我发现了这个javascript代码,它旨在通过阻止表单使用event.preventDefault()执行默认操作来允许自定义URL。然而,这要么不起作用,要么根本没有加载

这是我的代码:

    <script type="text/javascript">
    $(document).ready(function() {
    $("#propertysearch").submit(function(event) {
        event.preventDefault();
        window.location.href = "/"; //whatever i want but the problem is this doesnt seem to execute
    });
});
</script>
    <form id="propertysearch" action="/search" method="GET">
<p>
        <select id="maxprice" name="maxprice" style="width:47%;">
            <option value="10000000">Max Price...</option>
                <option disabled="disabled" value="10000000">- - - - - - - - - - - - - - - - - - - - - - -</option>
                <br />
<option value="100000">?100,000</option>
<option value="150000">?150,000</option>
<option value="200000">?200,000</option>
<option value="250000">?250,000</option>
        </select></p>
        <p><a href="javascript:document.forms['propertysearch'].submit();" title="Find a Property">Find a Property Now</a> &gt;</p>
        </form>

任何帮助都将不胜感激!

UPDATE我现在可以使用<input type="submit" value="Submit">而不是<a href="javascript:document.forms['propertysearch'].submit();" title="Find a Property">Find a Property Now</a> 来工作代码了

那么我如何更改我的<a></a>以使其在中工作

感谢您的帮助

示例中的代码使用的是jQuery。要么包括jQuery,要么使用非jQuery解决方案,比如

document.getElementById('propertytype').addEventListener('submit', function (event) {
    event.preventDefault();
    window.location.href = "/"; //whatever you want
});

请注意,以上内容不兼容跨浏览器;您还必须使用CCD_ 5。


要使用<a>,我只需要绑定到点击事件:

$("#propertysearch a").on('click', function (e) {
    event.preventDefault();
    //submission code
});
//pre jQuery 1.7
$("#propertysearch a").bind('click', function (e) {

从代码中删除代码:href="javascript:document.forms['propertysearch'].submit();",因此它应该是这样的:<p><a href='#' title="Find a Property">Find a Property Now</a></p>