如何使用Javascript更改动态HTML按钮onClick事件

如何使用Javascript更改动态HTML按钮onClick事件,javascript,html,Javascript,Html,我有密码: HTML: <div class="modal-footer" > <button type="button" class="btn btn-sm btn-default" id="mybutton" style="background-color: #367E2D; color:white; opacity: 1" onclick="chooseme();">OK</button> </div> ('#mybutton')

我有密码:

HTML:

<div class="modal-footer" >
     <button type="button" class="btn btn-sm btn-default" id="mybutton" style="background-color: #367E2D; color:white; opacity: 1" onclick="chooseme();">OK</button>
</div>
('#mybutton').click(chooseme('123'));
目的:

<div class="modal-footer" >
     <button type="button" class="btn btn-sm btn-default" id="mybutton" style="background-color: #367E2D; color:white; opacity: 1" onclick="chooseme();">OK</button>
</div>
('#mybutton').click(chooseme('123'));
我想使用Javascript将
onClick
事件从
choosme()
更改为
choosme('123')

上述代码的结果是:

<div class="modal-footer" >
     <button type="button" class="btn btn-sm btn-default" id="mybutton" style="background-color: #367E2D; color:white; opacity: 1" onclick="chooseme();">OK</button>
</div>
('#mybutton').click(chooseme('123'));
chooseme('123')立即执行(而不是在单击按钮时)

问题是:任何人都可以解释我的代码出了什么问题?这是什么 如何正确实施以达到上述目的


根据语法,我假设您正在使用jQuery。将事件绑定到JS中的命名函数时,需要将函数本身传递给回调。使用当前语法,将
chooseme()
中的返回值传递给
单击处理程序。因此,在页面加载时会立即调用该函数

将hander封装在匿名函数中,就可以开始了:

$('#mybutton').on('click', function() { chooseme('123') });

根据语法,我假设您正在使用jQuery。将事件绑定到JS中的命名函数时,需要将函数本身传递给回调。使用当前语法,将
chooseme()
中的返回值传递给
单击处理程序。因此,在页面加载时会立即调用该函数

将hander封装在匿名函数中,就可以开始了:

$('#mybutton').on('click', function() { chooseme('123') });

抱歉,我发现了一个问题,choseme执行了两次。我不知道为什么。无论如何,谢谢你的回答对不起,我发现了一个问题,choseme执行了两次。我不知道为什么。不管怎样,谢谢你的回答