2016-01-28 68 views
0

我試圖隱藏有兩個HTML數據的DIV例如下面的屬性具有工程師和紐約隱藏通過多數據屬性與jQuery/JavaScript的匹配現有的變量

<div class="row" data-job-type="Engineer" data-job-location="New York"></div> 
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland"></div> 
<div class="row" data-job-type="Wizard" data-job-location="New York"></div> 

的HTML數據屬性的一個下面我試圖以這種方式傳遞它,但是當我向它添加第二個屬性時它不起作用。

var bar = "New York"; 
var foo = "Engineer"; 

$('.row[data-job-type=' + foo + ']','.row[data-job-location=' + bar + ']').hide(); 
+0

檢查我答:) – Aswathy

回答

1

由於屬性值空間中的,具有被封閉在"'

此外,由於要選擇與這兩個屬性的元素,你需要把它們混合起來,如下

var bar = "New York"; 
 
var foo = "Engineer"; 
 

 
$('.row[data-job-type="' + foo + '"][data-job-location="' + bar + '"]').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row" data-job-type="Engineer" data-job-location="New York">1</div> 
 
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland">2</div> 
 
<div class="row" data-job-type="Wizard" data-job-location="New York">3</div>

0
<html> 
<head> 
    <title>sample Page</title> 
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> 

<script> 
    $(document).ready(function() { 

     $('.row[data-job-type="Engineer"][data-job-location="New York"]').hide(); 

    });  

</script> 
</head> 
<body> 
<div class="row" data-job-type="Engineer" data-job-location="New York">1111</div> 
<div class="row" data-job-type="Lockmsith" data-job-location="Maryland">22222</div> 
<div class="row" data-job-type="Wizard" data-job-location="New York">333333</div> 

</body> 
</html>