通过选择Onchange打开URL-jQuery [英] Onchange open URL via select - jQuery
本文介绍了通过选择Onchange打开URL-jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
附加事件的最佳方法是什么,以便更改选择选项的URL.将href储存在attr中,以备不时之需?
What would be the best way to attach an event so on change of a select option a URL. Store the href in an attr and grab it on change?
推荐答案
这很简单,让我们看一个可行的示例:
It is pretty simple, let's see a working example:
<select id="dynamic_select">
<option value="" selected>Pick a Website</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.youtube.com">YouTube</option>
<option value="https://www.gurustop.net">GuruStop.NET</option>
</select>
<script>
$(function(){
// bind change event to select
$('#dynamic_select').on('change', function () {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
});
</script>
$(function() {
// bind change event to select
$('#dynamic_select').on('change', function() {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
});
<select id="dynamic_select">
<option value="" selected>Pick a Website</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.youtube.com">YouTube</option>
<option value="https://www.gurustop.net">GuruStop.NET</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"
></script>
.
- 问题已经指定了jQuery .因此,我将其他选择排除在外.
- 在旧版本的jQuery(< 1.7)中,您可能希望将
on
替换为bind
. - 这摘录自 Meligy的Web开发人员新闻中的JavaScript提示.
- The question specifies jQuery already. So, I'm keeping other alternatives out of this.
- In older versions of jQuery (< 1.7), you may want to replace
on
withbind
. - This is extracted from JavaScript tips in Meligy’s Web Developers Newsletter.
.
这篇关于通过选择Onchange打开URL-jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文