选择在更改事件
问题描述:
我有以下几点:选择在更改事件
var deliveryform = ('form[id$="shipping_method"]');
$('body').on('change', (deliveryform "input[type=radio]"), {}, function(event) {
console.log('you changed the radio');
});
但我的选择不工作 - 我需要改变这里的东西吗?
答
您需要连接字符串选择器。尽管您提供了第三个参数,因为您根本不想传递任何数据,所以它根本不是必需的,因此为{}
。
var deliveryform = 'form[id$="shipping_method"]';
$('body').on('change', deliveryform + " input[type=radio]", function(event) {
// -----------^^^^^^^---------------^^^^^^-----
console.log('you changed the radio');
});
如果这些最初加载那么就没有必要的event delegation都只是处理程序直接绑定到的元素。
$('form[id$="shipping_method"] input[type=radio]').change(function(event) {
console.log('you changed the radio');
});
如果表单输入动态添加然后替换body
与形式选择器。
$('form[id$="shipping_method"]').on('change', "input[type=radio]", function(event) {
console.log('you changed the radio');
});
答
你的语法不太对。使用deliveryform
作为主选择器。试试这个:
var $deliveryform = $('form[id$="shipping_method"]');
$deliveryform.on('change', "input[type=radio]", function(event) {
console.log('you changed the radio');
});
答
您需要使用这样的:
var deliveryform = $('form[id$="shipping_method"]'); // was missing dollor sign
deliveryform.on('change', "input[type=radio]", {}, function(event) {
console.log('you changed the radio');
});
替代回答你的问题:
var deliveryform = 'form[id$="shipping_method"]'; // removed()
$('body').on('change', deliveryform + " input[type=radio]", {}, function(event) {
//. ^^------------------^^
console.log('you changed the radio');
});
答
- 你有你的
form
选择错过$
。 - 您已有
form
对象,那么为什么要绑定change
事件body
?只需使用表格!
var deliveryform = $('form[id$="shipping_method"]');
deliveryform.on('change', "input[type=radio]", function() {
console.log('you changed the radio');
});