Javascript jquery函数-多输入/1函数
我有以下3个动作,基本上是在单击标签时检查单选按钮。有没有办法把这些函数写成一个函数而不是三个单独的函数Javascript jquery函数-多输入/1函数,javascript,jquery,Javascript,Jquery,我有以下3个动作,基本上是在单击标签时检查单选按钮。有没有办法把这些函数写成一个函数而不是三个单独的函数 $("input[name='customOrder-qty']").on('click', function() { $('#qtyPackage-custom').prop('checked', true); }); $("input[name='customOrder-price']").on('click', function() { $('#qtyPacka
$("input[name='customOrder-qty']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
$("input[name='customOrder-price']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
$("input[name='customOrder-name']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
谢谢使用逗号分隔的选择器
或者更好地使用属性包含或以选择器开头
$("input[name*='customOrder']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
使用逗号分隔的选择器
或者更好地使用属性包含或以选择器开头
$("input[name*='customOrder']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
为每个输入添加一个公共类名。这使得扩展逻辑变得更加容易 HTML
为每个输入添加一个公共类名。这使得扩展逻辑变得更加容易 HTML
您可以采用以下几种方法: 一种是将事件的选择器组合在一起,以便
$("input[name='customOrder-qty'], input[name='customOrder-price'], input[name='customOrder-name']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
另一种是将绑定代码定义为实际函数,并从每个事件绑定调用该函数
$("input[name='customOrder-qty']").on('click', doStuff);
$("input[name='customOrder-price']").on('click', doStuff);
$("input[name='customOrder-name']").on('click', doStuff);
function doStuff()
{
$('#qtyPackage-custom').prop('checked', true);
}
或者,您可以将这两种方法结合使用,以实现以下功能
$("input[name='customOrder-qty'], input[name='customOrder-price'], input[name='customOrder-name']").on('click', doStuff);
function doStuff()
{
$('#qtyPackage-custom').prop('checked', true);
}
您可以采用以下几种方法: 一种是将事件的选择器组合在一起,以便
$("input[name='customOrder-qty'], input[name='customOrder-price'], input[name='customOrder-name']").on('click', function() {
$('#qtyPackage-custom').prop('checked', true);
});
另一种是将绑定代码定义为实际函数,并从每个事件绑定调用该函数
$("input[name='customOrder-qty']").on('click', doStuff);
$("input[name='customOrder-price']").on('click', doStuff);
$("input[name='customOrder-name']").on('click', doStuff);
function doStuff()
{
$('#qtyPackage-custom').prop('checked', true);
}
或者,您可以将这两种方法结合使用,以实现以下功能
$("input[name='customOrder-qty'], input[name='customOrder-price'], input[name='customOrder-name']").on('click', doStuff);
function doStuff()
{
$('#qtyPackage-custom').prop('checked', true);
}
请记住,jQuery选择器就像CSS选择器一样。我将使用下面Sushanth的答案。他演示的以选择器开头非常有用,因为所有输入名称值都以customOrder开头记住,jQuery选择器就像CSS选择器一样。他演示的选择器以选择器开头非常有用,因为所有输入名称值都以customOrder开头-