我正在尝试使用多个按钮发送套接字。我想避免很多声明,而是应用一个Class。 我试图使用onclick方法按钮没有成功。
<head>
script for jquery, dojo .....
<script type="text/javascript">
require(["dojo/ready"], function(ready){
ready(function(){
... socket onopen, onmessage etc.
$('#CommSend').onclick = function(e) {
var val = dijit.byId('CommandInput').get("value");
SendComm(val);
};
function SendComm(val){
socket.send(val);
$('#Messages').innerHTML += '\rSent: '+ val;
val = '';
};
....
});
});
</script>
<script type="text/javascript">
$('.ButtonEdit').onclick = function(e) {
SendComm($(this).attr('id'));
};
</script>
</head>
<body>
..
<input type="text" id="CommandInput">
......
<input type="button" id="CommSend"></input>
<input type="button" id="CommInit" class="ButtonEdit"></input>
<input type="button" id="CommBoot" class="ButtonEdit"></input>
....
</body>
只有“ CommSend”有效。我希望为每个单击的按钮发送带有“ id”的套接字。有什么解决办法吗?