admin管理员组文章数量:1418703
I am trying to use Javascript / JQuery to dynamically create several HTML buttons, each of which does something different when it is clicked.
My first attempt looked like this:
function myFunc( target_div, num_buttons ) {
var buttons="";
for ( var i=0; i<num_buttons; i++ ) {
buttons += '<input type="button" id="button_'+i+'" value="button_'+i+'"></input>';
}
target_div.html( buttons );
var doButtonPress = function( i ) {
alert( i ); // I actually do something more plicated here, but it's not important now
}
for ( var i=0; i<num_buttons; i++ ) {
$('#button_'+i).click( function() { doButtonPress(i); } );
}
}
Unfortunately this doesn't work because (I think) when the click event happens, doButtonPress is always passed the current value of i in myFunc's scope, not - as I intended - the value i had when the click event was defined. Thus the value passed to doButtonPress is always equal to num_buttons regardless of which button is pressed. So I then tried this instead:
$('#button_'+i).click( new Function( "doButtonPress("+i+");" ) );
Unfortunately this doesn't work either - I get a message "ReferenceError: Can't find variable: returnResult". I assume that's because the Function constructor's arg value isn't parsed until the click event happens, and at that time I'm already outside of myFunc's scope so doButtonPress is undefined? Am I getting this right so far?
So ... what do I do? :-)
I am trying to use Javascript / JQuery to dynamically create several HTML buttons, each of which does something different when it is clicked.
My first attempt looked like this:
function myFunc( target_div, num_buttons ) {
var buttons="";
for ( var i=0; i<num_buttons; i++ ) {
buttons += '<input type="button" id="button_'+i+'" value="button_'+i+'"></input>';
}
target_div.html( buttons );
var doButtonPress = function( i ) {
alert( i ); // I actually do something more plicated here, but it's not important now
}
for ( var i=0; i<num_buttons; i++ ) {
$('#button_'+i).click( function() { doButtonPress(i); } );
}
}
Unfortunately this doesn't work because (I think) when the click event happens, doButtonPress is always passed the current value of i in myFunc's scope, not - as I intended - the value i had when the click event was defined. Thus the value passed to doButtonPress is always equal to num_buttons regardless of which button is pressed. So I then tried this instead:
$('#button_'+i).click( new Function( "doButtonPress("+i+");" ) );
Unfortunately this doesn't work either - I get a message "ReferenceError: Can't find variable: returnResult". I assume that's because the Function constructor's arg value isn't parsed until the click event happens, and at that time I'm already outside of myFunc's scope so doButtonPress is undefined? Am I getting this right so far?
So ... what do I do? :-)
Share Improve this question asked Nov 2, 2012 at 16:22 baixiweibaixiwei 1,0294 gold badges20 silver badges27 bronze badges2 Answers
Reset to default 3Your code didn't work because of the scope of variable i
which would be num_button
always inside the handler. You can fix it by wrapping it inside a closure
but how about simplifying it by adding a class and making use of the id of the button.
function myFunc( target_div, num_buttons ) {
var buttons="";
for ( var i=0; i<num_buttons; i++ ) {
// added class ----------------v
buttons += '<input type="button" id="button_'+i+'" class="mybuttons" value="button_'+i+'"></input>';
}
target_div.html( buttons );
var doButtonPress = function( i ) {
alert( i ); // I actually do something more plicated here, but it's not important now
}
//yes, mybuttons exist
$('.mybuttons').click(function () {
doButtonPress(this.id.replace('button_', ''));
//this.id.replace('button_', '') <- returns i value
});
}
My suggestion would be to assign a data
attribute to each button which can then be read when the button is clicked. This will then avoid the need for unnecessary (and ugly) incremental id
attributes. Something like this:
function myFunc($target_div, num_buttons) {
var buttons = [];
for (var i = 0; i < num_buttons; i++) {
buttons.push($('<input type="button" class="button" data-id="' + i + '" value="button ' + i + '" />'));
}
$target_div.append(buttons);
var doButtonPress = function() {
alert($(this).data('id'));
}
$(".button").click(doButtonPress);
}
Example fiddle
本文标签:
版权声明:本文标题:Use JavascriptJQuery to dynamically create multiple HTML buttons with different click event handlers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745293842a2651958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论