如何根据之前定义的变量选择器选择具有属性的元素?

问题描述 投票:-1回答:1
var $label = $('label');

//How can I select a `label` using attribute selector, something like below.
$label['data-id=1'].text('some value');
//There is no such a syntax, I am just trying to explain what I need to do.

For more complete example, please visit this jsfiddle link

jquery variables jquery-selectors
1个回答
0
投票

您可以使用filter$label获取匹配的ID标签,然后设置文本值。参见下面的代码

 var id = $(this).find('.form-control-data-id').text();
  var val = $(this).find('.form-control').val();
  $label.filter(function(){
    return id == $(this).data('id');
  }).text(val);

JSFiddle Demo

© www.soinside.com 2019 - 2024. All rights reserved.