jQuery在选择选项中导航到页面
我试图编写一个小片段,允许用户根据选项选择控件中的用户选择转到(即导航到)一个新urljQuery在选择选项中导航到页面,jquery,Jquery,我试图编写一个小片段,允许用户根据选项选择控件中的用户选择转到(即导航到)一个新url <script type="text/javascript"> /* <[CDATA[ */ jQuery.noConflict(); jQuery(document).ready(function(){ var id = jQuery($this).val(); var url = some_Lookup_func(id); jQuery("#the_id").ch
<script type="text/javascript">
/* <[CDATA[ */
jQuery.noConflict();
jQuery(document).ready(function(){
var id = jQuery($this).val();
var url = some_Lookup_func(id);
jQuery("#the_id").change(function () { /* how do I navigate the browser to 'url' ?*/ );
});
/* ]]> */
</script>
/* */
注意:这不是我想要的AJAX行为,我希望浏览器的行为就像你点击了超链接一样。我以前做过这件事,但我忘了怎么做。我查看了jQuery文档,load()似乎没有这样做——因为我不想将内容放在当前页面中——我想:
window.location.href = '/newlocation?param1=value1';
要简单地重新加载页面,请将
window.location.href
设置为您的URL。要传递参数,请构建一个“name=value&name=value”字符串,并将其固定到末尾。jQuery提供了一个“serialize”方法,使之易于实现(好像很难开始)
jQuery("#the_id").change(function () {
document.location.href = 'url here' + $(this).val();
};
更多信息:
jQuery
var YourParam="sunshine";
$(document).ready(function() {
$("#goto").change(function(){
if ($(this).val()!='') {
window.location.href=$(this).val()+"?param="+YourParam;
}
});
});
HTML
<form action="whatever.shtml" method="post" enctype="multipart/form-data">
<select id="goto">
<option value="">Go somewhere...</option>
<option value="http://cnn.com/">CNN</option>
<option value="http://disney.com/">Disney</option>
<option value="http://stackoverflow.com/">stackoverflow</option>
<option value="http://ironmaiden.com/">Iron Maiden</option>
</select>
</form>
去某个地方。。。
有线电视新闻网
迪士尼
栈溢出
铁娘子
我想这是最好的方法(根据格特的回答):
谢谢你,我觉得这些非常有用!
<form action="whatever.shtml" method="post" enctype="multipart/form-data">
<select id="goto">
<option value="">Go somewhere...</option>
<option value="http://cnn.com/">CNN</option>
<option value="http://disney.com/">Disney</option>
<option value="http://stackoverflow.com/">stackoverflow</option>
<option value="http://ironmaiden.com/">Iron Maiden</option>
</select>
</form>
$(document).on('change','#goto', function(event){
if ($(event.target).val()!='') {
window.location.href=$(event.target).val();
}
});