jQuery事件不工作的单选按钮?



我有一个问题,jQuery事件监听器,我附加到一个单选按钮不工作

我想能够抓取单个单选按钮的值CHANGE. 不幸的是,我试过的所有方法都不起作用。我在这里查了一些答案…

下面是我的jQuery代码
$('input[type=radio][name=method]').click(function() {
let selectedMethod = $('input[type=radio][name=method]:checked').val();
console.log(selectedMethod);
});

我也试过这个

$('input[type=radio][name=method]').change(function() {
let selectedMethod = $('input[type=radio][name=method]:checked').val();
console.log(selectedMethod);
});

我也试过从这里的答案

$('input#method').on('ifChecked', function() {
let selectedMethod = $(this).val();
console.log(selectedMethod);
});

下面是HTML单选按钮。单选按钮的自定义样式为CARDS

<label>
<input type="radio" name="method" id="method" value="pp"
class="card-input-element" required />
<div class="card card-default card-input">
<div class="card-body text-center">
<img src="img/paypal.png" width="50" /> <br><b>Paypal</b>
</div>
</div>
</label>

None正在工作。我不知道我做错了什么。

您尝试的第二种方法完全有效。

确保选择器选择了正确的id。这就是我所做的。HTML:

<label>
<input type="radio" name="method" id="method" value="pp" class="card-input-element" required />
<div class="card card-default card-input">
<div class="card-body text-center">
<img src="img/paypal.png" width="50" /> <br><b>Paypal</b>
</div>
</div>
</label>

Jquery:

//test id make sure the selector is correct..
$('#method').change(function() {
let selectedMethod = $('input[type=radio][name=method]:checked').val();
//this console logs "pp"
console.log(selectedMethod);
});

最新更新