2013-02-05 48 views
0

我有一个表单,在我提交它的时刻,它将我带到一个网址www.domain.com/search/?maxprice=10000000,但是我希望它做的是带我去一个自定义的网址例如www.domain.com/search/maxprice_10000000/从表单提交自定义查询字符串

我发现这个JavaScript代码,这是意味着允许自定义网址,通过使用event.preventDefault()做它的默认操作停止形式。然而这个心不是任何工作或心不是装载在首位

这里是我的代码:

<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>,使工作它的工作

感谢您的帮助

+0

哥们,我已经发布了解决方案,但你没有注意到它。 – OQJF

回答

1

的你的例子中的代码使用jQuery。要么包括jQuery的,或与非jQuery的溶液状,从而去:

document.getElementById('propertytype').addEventListener('submit', function (event) { 
    event.preventDefault(); 

    window.location.href = "/"; //whatever you want 
}); 

注意,上面没有跨浏览器兼容;你将不得不使用attachEvent


要使用<a>,我只想结合的Click事件:

$("#propertysearch a").on('click', function (e) { 
    event.preventDefault(); 

    //submission code 
}); 

//pre jQuery 1.7 
$("#propertysearch a").bind('click', function (e) { 
+0

我不能得到这个工作,它仍然只是提交querystring,你是什么意思,我将不得不使用'attachEvent' – braza

+0

@braza Internet Explorer使用'attachEvent',但其他浏览器倾向于使用'addEventListener'。你把这个包装在'document.ready'中了吗? –

+0

我的脚本代码现在看起来像这样:\t'$(document).ready(function(){ document.getElementById('propertysearch')。addEventListener('submit',function(event){ event.preventDefault(); window.location.href = “/图”; //任何你想要的 }); });' 做我想改变这行'

'? – braza

0

删除代码:HREF =“JavaScript的:document.forms [ 'propertysearch']提交(); “从你的代码,所以它应该是这样的:<p><a href='#' title="Find a Property">Find a Property Now</a></p>

相关问题