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
Add a ment  | 

4 Answers 4

Reset to default 6

By 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