2013-02-05 49 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>

相關問題