将功能绑定到事件“onclick”一组按钮的元素 [英] Bind function to the event "onclick" of the elements of an array of buttons
问题描述
前言:我是意大利人,对不起我的英文不好。
这是我的问题:
我想给一组按钮分配一个函数。
我需要发送一个参数给函数。
function test(atxt){
var buttons = $( 'tblButton。');
for(var i = 0; i< buttons.length; i ++){
buttons [i] .onClick(sayHello(atxt));
函数sayHello(txt){alert('hello'+ txt)};
...出现以下错误:
Uncaught TypeError:Object#< HTMLButtonElement>没有方法'onClick'
你能告诉我我哪里出错了,我该如何解决它?
编辑:我需要迭代,因为我需要按钮的id作为函数的参数,所以我需要做按钮[i] .onClick(sayHello(buttons [i] .id))
你的例子:你有迭代的另一个原因吗?
function test(atxt){
$('。 tblButton '(点击,函数())上。'{的sayHello(atxt);});
}
函数sayHello(txt){alert('hello'+ txt)};
或者,如果元素是静态的并且存在:
function test(atxt){
$('。tblButton')。click(function(){sayHello(atxt);});
}
函数sayHello(txt){alert('hello'+ txt)};
替代方法:只需将
更改为此样式即可:
var txt =fred;
var atext =hello+ txt;
函数sayHello(atext){
alert(atext); ('click',function(){
sayHello(atext);
});
$('。tblButton')。
//这里仅仅是为了演示
$('。tblButton')。eq(0).click(); //用fred
触发txt =Johnny; //新文本
text ='hello'+ txt;
$('。tblButton')。eq(1).click(); //触发Johnny
看到它在这里工作:
http://jsfiddle.net/dFBMm/
基于您的笔记:
此标记和代码:
< button class =tblButtonid =Ruth>嗨< / button>
< button class =tblButtonid =Betty>再次嗨< / button> $('。tblButton')。on('click',function(){
alert(Hello+ $(this).attr(id));
});
$('。tblButton')。eq(0).click(); //用Ruth
$('。tblButton')。eq(1).click(); //触发贝蒂
Preamble: I'm Italian, sorry for my bad English.
This is my problem:
I want to assign a function to a set of buttons.
I need to send a parameter to the function.
this is the code that I've tried:
function test(atxt) {
var buttons = $('.tblButton');
for (var i = 0; i < buttons.length; i++) {
buttons[i].onClick(sayHello(atxt));
}
}
function sayHello(txt){alert('hello' + txt)};
...getting the following error:
Uncaught TypeError: Object #<HTMLButtonElement> has no method 'onClick'
can you tell me where I went wrong and how can I fix it?
EDIT: I need iteration because I need the 'id of the button as a parameter of the function so i need to do buttons[i].onClick(sayHello(buttons[i].id))
Would this not work for your example: Do you have another reason for the iteration?
function test(atxt) {
$('.tblButton').on('click',function(){sayHello(atxt);});
}
function sayHello(txt){alert('hello' + txt)};
OR optionally if the elements are static and present:
function test(atxt) {
$('.tblButton').click(function(){sayHello(atxt);});
}
function sayHello(txt){alert('hello' + txt)};
Alternate approach: just change to this style:
var txt = "fred";
var atext = "hello" + txt;
function sayHello(atext) {
alert(atext);
}
$('.tblButton').on('click', function() {
sayHello(atext);
});
//below here just to demonstrate
$('.tblButton').eq(0).click();//fires with the fred
txt = "Johnny";// new text
atext = 'hello' + txt;
$('.tblButton').eq(1).click();//fires the Johnny
see it working here: http://jsfiddle.net/dFBMm/
SO based on your note: this markup and code:
<button class="tblButton" id="Ruth">Hi</button>
<button class="tblButton" id="Betty">Hi again</button>
$('.tblButton').on('click', function() {
alert("Hello "+$(this).attr("id"));
});
$('.tblButton').eq(0).click();//fires with the Ruth
$('.tblButton').eq(1).click();//fires the Betty
这篇关于将功能绑定到事件“onclick”一组按钮的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!