admin管理员组文章数量:1291007
<script>
function go()
{
window.location=document.getElementById("menu").value;
}
</script>
<body>
<form>
<select id="menu" onchange="go()">
<option>--Select a page--</option>
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>
</form>
</body>
this works perfectly, but I cant call the function from External File
$(document).ready(function() {
function go(){
window.location=document.getElementById("menu").value;
}});
<script>
function go()
{
window.location=document.getElementById("menu").value;
}
</script>
<body>
<form>
<select id="menu" onchange="go()">
<option>--Select a page--</option>
<option value="http://www.1.">1</option>
<option value="http://www.2.">2</option>
<option value="http://www.3.">3</option>
</select>
</form>
</body>
this works perfectly, but I cant call the function from External File
$(document).ready(function() {
function go(){
window.location=document.getElementById("menu").value;
}});
Share
Improve this question
asked Feb 24, 2014 at 14:17
FaizanFaizan
7762 gold badges7 silver badges19 bronze badges
5
- what do you mean by external file?? – Milind Anantwar Commented Feb 24, 2014 at 14:18
- jQuery or JavaScript file – Faizan Commented Feb 24, 2014 at 14:18
- You didnt close the document ready function properly...$(document).ready(function() { function go(){ window.location=document.getElementById("menu").value; }}); – kamesh Commented Feb 24, 2014 at 14:19
-
1
Simply call it like you are, just make sure you include the
script
AFTER your HTML – tymeJV Commented Feb 24, 2014 at 14:19 - What does the console say? – Jite Commented Feb 24, 2014 at 14:22
4 Answers
Reset to default 6By putting function go(){}" inside of "$(document).ready(function() {
you enclosing the the go function within the scope of $(document).ready(function() {}
Use document.ready()
to call a function, not to declare it.
Add an Event-Hanlder to your Script like
$('#menu').on('onchange', function()
{
// do something
}
Example:
$(document).ready(function() {
$('#menu').on('onchange', function()
{
window.location=document.getElementById("menu").value;
}
});
And your HTML looks like:
<select id="menu">
<option>--Select a page--</option>
<option value="http://www.google.">1</option>
<option value="http://www.2.">2</option>
<option value="http://www.3.">3</option>
</select>
No it's jquery but it's very simple!
<select id="menu" onchange="document.location.href = this.value">
<option>--Select a page--</option>
<option value="http://www.google.">1</option>
<option value="http://www.2.">2</option>
<option value="http://www.3.">3</option>
</select>
Use $('#menu').find(":selected").text()
;
本文标签: javascriptHow to call jQuery Function from HtmlStack Overflow
版权声明:本文标题:javascript - How to call jQuery Function from Html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524236a2383371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论