3

Here's the scenario: I have a set of buttons that I want to bind to corresponding functions when clicked. The ids of these buttons are the same as the names of their corresponding functions. I could do this:

$("#kick").click(kick);
$("#push").click(push);
$("#shove").click(shove);

But I'm lazy and would like to do this more lazily (as is my nature). As the buttons are all contained in a block element, I'd like to do something like this:

$("#button_holder > span").each(function () {
    var doThis = this.id;
    $(this).click(doThis);
});

Except that doesn't work. Any suggestions?

4

5 回答 5

7

Like the other answerers, I'm not sure that this is "true" laziness (as in the programmer's virtue), but only you know your implementation details.

If you really want to do something like this, you can use an object to store your particular functions and then use the string to look them up:

var myFunction = {
     kick:  kick,
     push:  push,
     shove: shove
};

$("#button_holder > span").each(function () {
    var doThis = this.id;
    $(this).click(myFunction[doThis]);
});

Or as anonymous functions:

var myFunction = {
     kick:  function() { /*do kick  */ },
     push:  function() { /*do push  */ },
     shove: function() { /*do shove */ }
};

$("#button_holder > span").each(function () {
    var doThis = this.id;
    $(this).click(myFunction[doThis]);
});
于 2008-12-28T15:56:16.640 回答
0
$("#button_holder > span").each(function () {
    var doThis = this.id;
    $(this).click(function(doThis){
      alert(doThis);
    });
});
于 2008-12-28T15:24:56.313 回答
0
<input type="submit" id="kick" value="Kick Me">
<input type="submit" id="push" value="Push Me">

<script type="text/javascript>
$(function() {
    $("input[type=submit]").each(function () {
        var doThis = this.id;
        $(this).click(doThis);
    });
});
</script>
于 2008-12-28T15:31:47.500 回答
0

I guess I don't see the point of this. You need to create each function individually anyway and this sort of programming is difficult for others to grok usually. Maybe if you have dozens of buttons and it's throw-away code, I could understand but .. probably not...

Why not just bind all to a single function and make the determination based on this.id at event time??

于 2008-12-28T15:35:51.610 回答
0

its very simple ! you try to pass none-function to the click method.

var doThis = this.id;
$(this).click(doThis);

you need to pass a function :

 $(this).click(function(doThis){
      alert(doThis);

});

or :

function myFunc(){};
$(this).click(myFunc);
于 2008-12-28T15:46:02.947 回答