如何 select 具有基于变量 select 或先前定义的属性的元素?

How to select an element with attribute based on variable selector which is defined earlier?

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

您可以利用filter$label获取匹配的id标签,然后设置文本值。看下面的代码

  var $label = $('label');

  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