你能帮我简化这个选择器吗?
这是代码:
$("input[name='grpQuestion1'], input[name='grpQuestion2'], input[name='grpQuestion4']").change(function (event) {
/* Some code here */
});
你能帮我简化这个选择器吗?
这是代码:
$("input[name='grpQuestion1'], input[name='grpQuestion2'], input[name='grpQuestion4']").change(function (event) {
/* Some code here */
});
试试这个
$('input[name^="grpQuestion"]').change(function (event) { /* Some Code Here */ });
jquery 有一些属性选择
比如$("[attribue^=value]")
属性以值词开头的元素。
$("[attribue$=value]")
属性以值词结尾的元素。
$("[attribue*=value]")
属性包含作为部分或单词或确切单词的值的元素。
$("[attribue~=value]")
属性包含值作为精确词的元素
$('input[name^="grpQuestion"]').change(/** your code */)
它所做的基本上是选择所有以“grpQuestion”开头的元素
你可以试试这个
$('input[name^="grpQuestion"]').change(function (event) { /* Some Code Here */ });
方法
输入以 name="grpQuestion" 开头
试试这个
$("input[name^=grpQuestion]").change(function (event) {
/* Some Code Here */ });
要选择名称属性以“grpQuestion”开头的前四个输入元素,可以使用以下 jQuery 选择器:
$('input[name^="grpQuestion"]:lt(4)')
你在评论中说你需要这三个名字,所以
var names = ['grpQuestion1', 'grpQuestion2', 'grpQuestion4'];
$.each(names, function(i, v) {
$('input[name='+v+']').change(function (event) {
/* Some Code Here */
});
});
会使其更容易维护。