admin管理员组

文章数量:1314442

In this jsFiddle

am I trying to pass an argument to a function, but it doesn't receive the argument or it isn't executed.

<a href="javascript:addRemove('7249');">Details</a>

JQuery

$(document).ready(function() {

    function addRemove(u) {
    alert(u);
    }

});

Any ideas what's wrong and how to fix it?

In this jsFiddle

am I trying to pass an argument to a function, but it doesn't receive the argument or it isn't executed.

<a href="javascript:addRemove('7249');">Details</a>

JQuery

$(document).ready(function() {

    function addRemove(u) {
    alert(u);
    }

});

Any ideas what's wrong and how to fix it?

Share Improve this question edited Jun 20, 2011 at 16:20 love Computer science 1,8284 gold badges20 silver badges39 bronze badges asked Jun 20, 2011 at 14:15 Sandra SchlichtingSandra Schlichting 26.1k38 gold badges121 silver badges185 bronze badges 2
  • $(document).ready(function() { ... }) creates a scope out of which functions can't escape (the result is that you can't call them by their name from the global context). Take addRemove out of it and it'll work. – zneak Commented Jun 20, 2011 at 14:17
  • There are better solutions though (like event handlers). Someone's bound to post an answer with a decent amount of detail, so you might as well wait for it. – zneak Commented Jun 20, 2011 at 14:19
Add a ment  | 

8 Answers 8

Reset to default 6

Your function only exists within the scope of the ready event handler, you need to move function addRemove outside of the ready function.

http://jsfiddle/EcCTx/2/

Your code was wrapped in an onload event by jsfiddle (drop-down menu on the left). So if you add a function it won't be global, but your onclick event calls a global function by the name addRemove.

You need to define your function outside of the $(document).ready().

I haven't tested it, but my guess is this: things inside of a function can't be accessed from outside of a function. For example,

$(document).ready(function() {
    function addRemove(u) {
        alert(u);
    }
});
console.log(addRemove); // reference error or something similar

You should define addRemove function outside of $(document).ready.

the addRemove function must be outside of $(document).ready(function(){...});

In case Davin doesn't e back, here's the answer: jsFiddle defaults to wrapping your JS in the 'onLoad' method - and we can't allow that.

http://jsfiddle/nqbWe/

You had no defined function called addRemove in the Fiddle!

I've added this, and removed the inline javascript calls.

See this for better way of doing it:

http://jsfiddle/EcCTx/6/

There is nothing specifically calling that function. In the document ready part you have the function set up, but the anchor will not call that function by itself. In this instance it will only be called when someone clicks on that link.

You could give the link a class and data attribute and use those with jQuery to have something happen on page load.

本文标签: javascriptPassing argument to function What39s wrongStack Overflow