如何让动态Javascript/JQuery事件监听器知道单击哪个单选按钮组并检索已检查元素的值.例如,如果我有两个单选按钮组,我怎么知道哪一个被点击?如何为动态数量的按钮组执行此操作?
两个单选按钮组的示例:
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<input type="radio" name="input1" value="option1"/>
<input type="radio" name="input1" value="option2"/>
</div>
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<input type="radio" name="input2" value="option1"/>
<input type="radio" name="input2" value="option2"/>
</div>
Run Code Online (Sandbox Code Playgroud)
编辑:对于其他任何想知道下面到目前为止下面哪个问题是"更"正确的人,两者都是正确的,因为它.change(function(e) {是一个捷径.on('click', function(e) {
编辑2:删除了ID
重申这个问题,为什么数组中重复的哈希条目引用了Perl中的第一个条目?如果我弄错了,请更正我的术语,但是当我将相同的哈希引用推送到具有以下代码的数组中时:
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my @array;
my %hash = (foo => 'foo', bar => 'bar');
for (1..3) {
push @array, \%hash;
}
print Dumper @array;
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
$VAR1 = {
'bar'=> 'bar',
'foo'=> 'foo'
};
$VAR2 = $VAR1;
$VAR3 = $VAR1;
Run Code Online (Sandbox Code Playgroud)
我希望看到以下结果:
$VAR1 = {
'bar'=> 'bar',
'foo'=> 'foo'
};
$VAR2 = {
'bar'=> 'bar',
'foo'=> 'foo'
};
$VAR3 = {
'bar'=> 'bar',
'foo'=> 'foo'
};
Run Code Online (Sandbox Code Playgroud)
这种行为是因为Perl的基本概念还是因为Data::Dumper?
我有这个脚本,应该在单击按钮时更改按钮的文本.
<body>
<button onclick="toggleText(this);" id="id">Edit</button>
</body>
function toggleText(element){
var text = document.getElementById(element.id).textContent;
if (text == 'Edit') {
text = 'Done';
} else {
text = 'Edit';
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.它只适用于document.getElementById(element.id).textContent直接放入if语句的情况.
如何正确存储变量?