Javascript-基于下拉选择的转到URL(续!) [英] Javascript - Goto URL based on Drop Down Selections (continued!)
问题描述
此操作从此处继续: Javascript/jQuery-Goto基于下拉选择的URL
一些很棒的人给了我一些代码示例,但是我在将代码插入示例表单中的位置时遇到了真正的麻烦.
Some great folks have given me some code examples, but I am having real trouble on where to insert the code in my example form.
有人可以让我知道应该以表格的形式正确添加上一个问题位置中提供的代码的正确位置吗?
Can someone let me know where exactly the code provided in the previous question location should be added in a form to work properly?
推荐答案
您需要在JavaScript文件中的某个位置将函数绑定到表单的 onsubmit
事件,以便它可以执行您想要的任何操作
Somewhere in your JavaScript file you need to bind a function to the onsubmit
event of your form, so it can do whatever you want.
如果您使用的是jQuery,请执行以下操作:
If you are using jQuery do this:
$(function(){
$('form').submit(function(e){
window.location.href = $('#dd1').val() +
$('#dd2').val()+
$('#dd3').val();
e.preventDefault();
});
});
在此处检查其工作方式: http://jsfiddle.net/WDtGK/2/
Check how it works here: http://jsfiddle.net/WDtGK/2/
为上下文添加了HTML
added HTML for context
<form>
<select class="dropdown" id="dd1">
<option>http://</option>
<option>ftp://</option>
<option>https://</option>
</select>
<select class="dropdown" id="dd2">
<option>google</option>
<option>yahoo</option>
<option>bbc</option>
<option>hotmail</option>
</select>
<select class="dropdown" id="dd3">
<option>.com</option>
<option>.net</option>
<option>.co.uk</option>
</select>
<input type="submit" name="button" id="button" value="Go!">
</form>
这篇关于Javascript-基于下拉选择的转到URL(续!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!