我希望将用户重定向到选择值中指定的 URL。对于选择的样式,我必须采用自定义路线,因此采用其设置方式。我是不是处理错了,如果是,最有效的处理方法是什么?
HTML
<form>
<span class="dropdown-el" onChange="window.document.location.href=this.options[this.selectedIndex].value;">
<input type="radio" name="services" value="Relevance" checked="checked" id="services">
<label for="services">Select one ...</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-best"><label for="sort-best">Option 1</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-low"><label for="sort-low">Option 2</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-high"><label for="sort-high">Option 3</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-brand"><label for="sort-brand">Option 4</label>
<input type="radio" name="services" value="http://www.google.com" id="sort-name"><label for="sort-name">Option 5</label>
</span>
<button type="submit" class="hero-form-btn" onClick="WinOpen();">Go →</button>
</form>
Jquery/JavaScript
<script>
jQuery(function($) {
$('span').on('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
</script>
我已经看了很多遍了,以至于我开始迷路了。任何帮助深表感谢。
最佳答案
您应该从选定的单选按钮中获取值:
jQuery(function($) {
$('span').on('change', function(val) {
var url = $("input[name='services']:checked").val();
if (url) {
window.location = url;
}
return false;
});
});
你可以在这里测试: https://jsfiddle.net/6k8cnxhp/
关于javascript - 在选择提交时重定向到值 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59439456/