开发者

Problem with each in array inputs

开发者 https://www.devze.com 2023-03-21 12:24 出处:网络
Hey guys i have this inputs <input name=\'text[en]\' value=\'aaaaaa\' /> <input name=\'text[fr]\' value=\'bbbb\' />

Hey guys i have this inputs

<input name='text[en]' value='aaaaaa' />
<input name='text[fr]' value='bbbb' />

I can obtain values with each using $(this).val(), 开发者_StackOverflow社区but how i can obtain en,es that are inside de name


This will do it

var lang = this.name.match(/\[(.*)\]/)[1];


$('input').each(function () {
    alert($(this).attr('name').match(/text\[([a-z]{2})\]/)[1]);
})


this.name

Will get you the full string like 'text[en]'. Then you can use a regex or substring to find the two letter code you are looking for.

An example would be:

var code = this.name.substring(5,6);

EDIT: Updated to reflect Gedrox's simplification.


Try this

$("input").each(function(){
   var name = $(this).attr("name");
   name = name.substring(name.indexOf("[") + 1, name.indexOf("]") - name.indexOf("]") -1);
});


You could do:

      var name = $(this).attr('name');
      var name = name.slice(-3, -1);


Use this to match the name to the regex

var match = /(.*)\[([^\]]+)\]/.exec($(this).attr('name'));

match[0] will contain the part before the square brackets, match[1] will contain the shorthand language code.

0

精彩评论

暂无评论...
验证码 换一张
取 消