本文介绍了Javascript - 从选择菜单获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! $ c>< select name =Pcolorid =imagestyle =height:30px;> < option value =>评论:Blue / Lt Blue< / option> < option value =45751303color-number =0>黑色< / option> < option value =45751343color-number =1> Black / Pink< / option> < option value =45751359color-number =2> Blue / Lt Blue< / option> < option value =45751324color-number =3> Dk Purple / Purpl< / option> < option value =45751390color-number =4> Ink / Cerise< / option> < / select> 我想问我如何得到 color-number 属性编号,其中选项文本是蓝色/蓝色例如? 它是非常重要,只能使用JavaScript / jQuery! 我希望你明白我想要描述的内容,提前感谢! b 解决方案使用:contains()不会是一个正确的解决方案,因为它会返回部分匹配,您可以使用 .filter() var text ='Black / Pink'; var cn = $('select [name =Pcolor] option')。 filter(function(){return $(this).text()。trim()== text;})。attr('color-number'); console.log(cn) < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min .js>< / script>< select name =Pcolorid =imagestyle =height:30px;> < option value =>注意:Blue / Lt Blue< / option> < option value =45751303color-number =0>黑色< / option> < option value =45751343color-number =1> Black / Pink< / option> < option value =45751359color-number =2> Blue / Lt Blue< / option> < option value =45751324color-number =3> Dk Purple / Purpl< / option> < option value =45751390color-number =4> Ink / Cerise< / option>< / select> Hello there dear community.Here is my HTML code:<select name="Pcolor" id="image" style="height:30px;"> <option value="">Избран цвят: Blue/Lt Blue </option> <option value="45751303" color-number="0">Black</option> <option value="45751343" color-number="1">Black/Pink</option> <option value="45751359" color-number="2">Blue/Lt Blue</option> <option value="45751324" color-number="3">Dk Purple/Purpl</option> <option value="45751390" color-number="4">Ink/Cerise</option></select>I want to ask how i can get the color-number attribute number where option text is Blue/Lt Blue for example ?It is important to make it only with JavaScript/jQuery !I hope you understand what i want to describe, thanks in advance! 解决方案 Using :contains() won't be a proper solution as it will return partial matches, instead you can use .filter()var text = 'Black/Pink';var cn = $('select[name="Pcolor"] option').filter(function() { return $(this).text().trim() == text;}).attr('color-number');console.log(cn)<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><select name="Pcolor" id="image" style="height:30px;"> <option value="">Избран цвят: Blue/Lt Blue</option> <option value="45751303" color-number="0">Black</option> <option value="45751343" color-number="1">Black/Pink</option> <option value="45751359" color-number="2">Blue/Lt Blue</option> <option value="45751324" color-number="3">Dk Purple/Purpl</option> <option value="45751390" color-number="4">Ink/Cerise</option></select> 这篇关于Javascript - 从选择菜单获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 01:03