很抱歉打扰您,但这让我很困扰:我有以下变量:
var one = 1;
var two = 2;
var tre = 3;
我也有输入框
<input type='text' id='one' class='some'>
我想要一个包含上述变量对应值的变量,基于输入框的 id。
var atr = $(".some").attr("id");
var nr = ???
我希望 varnr
等于 1 (作为上面的变量 )one
很抱歉打扰您,但这让我很困扰:我有以下变量:
var one = 1;
var two = 2;
var tre = 3;
我也有输入框
<input type='text' id='one' class='some'>
我想要一个包含上述变量对应值的变量,基于输入框的 id。
var atr = $(".some").attr("id");
var nr = ???
我希望 varnr
等于 1 (作为上面的变量 )one
使用以下符号:
$('selector[attr]') // elements that has attribute attr
$('selector[attr=value]') // elements that has attribute attr with value `value`
$('selector[attr^=value]') // elements that has attribute attr starting with value `value`
$('selector[attr$=value]') // --~-- ending with `value`
$('selector[attr!=value]') // not equal `value`
$('selector[attr*=value]') // attribute contains `value`
$('selector[attr|=value]') // attribute has prefix `value`
$('selector[attr~=value]') // attribute contain word delimited by spaces
查看jquery 属性选择器的完整列表
编辑:您可能会问另一件事:
您有特定的值映射并希望这样做:
var map = { one: 1, two : 2 , three: 3 } ,
elem = $('.some'),
attr = elem.attr('id'),
nr = map[attr] // === 1
您只能使用动态生成的变量名称来匹配对象属性:
var ids = { one : 1, two : 2, tre : 3 };
var atr = $(".some").attr("id");
var nr = ids[atr]; // now contains 1