从表单提交自定义查询字符串
Submitting a custom query string from form
我有一个表单,当我提交它的时候,它会把我带到网址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> ></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>
相关文章:
- 如何有效地将游戏数据存储在URL查询字符串中
- 从查询字符串参数推断出正确的数据类型
- AngularJS:获取链接中没有哈希的查询字符串值
- AngularJS,如何更改查询字符串
- Datatables查询url字符串:需要regex来修复url解析
- 如何将activeadmin查询字符串传递给batch_action
- 如何从查询字符串中的输入字段发回文本
- Django在使用AJAX时没有接收到新的查询字符串
- 使用查询字符串方法清除浏览器缓存
- 用于刷新的Javascript更改查询字符串
- 我想将查询字符串变量添加到JSON名称/值对中
- 将查询字符串值从页面传递 asp.net 到 iframe
- jqueryajax生成查询字符串,如果未定义则不通过
- 如何将查询字符串传递到HTML框架
- iron路由器将参数添加到字符串中,并添加一个查询
- 使用字符串查询此选择器
- 替换URL'中的字符串查询;s与Javascript(有条件)
- 基于匹配或正则表达式的查询字符串查询Mongo
- 简单json字符串查询
- 向Django传递一个布尔字符串查询