jQuery select with partial name

て烟熏妆下的殇ゞ 提交于 2019-12-23 06:57:48

问题


I'd like the count the element on the page with the name :

  • MyElement_1
  • MyElement_2

Then I'd like get element MyElement

Thanks,


回答1:


var elements = $('[name^=MyElement]');
var length = elements.length;

If you want some more advanced filtering, you can filter all the dom nodes by matching your own rules :

var elements = $('[name]').filter(function(){
    return /^MyElement_\d+$/.test($(this).attr('name'));
});



回答2:


Use starts with selector:

$('[name^="MyElement"]').length



回答3:


better using

$('input[name$="name"]')



回答4:


To find an element by the name attribute, use:

$('[name=something]'); 

use *=, ^=, etc. to be more or less specific in the search term. Look at the jquery selectors page for more info on how to use them.

The exaple above returns all elements that match. Suffix this with .length and you'll get the number of elements that are found, like so:

 $('[name=something]').length;


来源:https://stackoverflow.com/questions/7873782/jquery-select-with-partial-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!