我可以很好地得到结果,但是每个数据值的结果都是逗号分隔的,因为它是一个数组。我想不带任何逗号进行查询,并可能删除最后一个AND。
那可能吗 ?
jQuery的
//Submit Adv Search
$('#submitAdvSearch').click(function(){
var query = "SELECT * FROM table WHERE ";
var yo = $('.subCat span[data-value].checked').map(function(){
return $(this).attr('data-value')+'=1 AND ';
}).get();
console.log(query+' '+yo);
});
的HTML
<div class="cat">
<div class="catTitle">
<h2>Performance</h2>
<span class="arrow"></span>
<br class="clear"/>
</div>
<div class="subCat" data-cat="performance">
<div>
<span data-all="all">○ Tous</span><br>
<div class="after">
<span data-value="30000_doublefrotements" class="checked">○ +30,000 double frottement</span><br>
<span data-value="atcc96_lavablecommercial">○ ATTCC 96</span><br>
<span data-value="nfpa701_inifuge">○ NFPA 701</span><br>
<span data-value="resistanrayonsuv">○ Uv résistant</span>
</div>
</div>
</div>
</div>
<div class="cat">
<div class="catTitle">
<h2>Style</h2>
<span class="arrow"></span>
<br class="clear"/>
</div>
<div class="subCat" data-cat="style">
<div>
<span data-all="all">○ Tous</span><br>
<div class="after">
<span data-value="contemporain" class="checked">○ Contemporain</span><br>
<span data-value="traditionnel">○ Traditionel</span><br>
<span data-value="transitionnel" class="checked">○ Transitionel</span>
</div>
</div>
</div>
</div>
<div id="submitAdvSearch">Submit</div>
结果
SELECT * FROM table WHERE 30000_doublefrotements=1 AND ,contemporain=1 AND ,transitionnel=1 AND
最佳答案
使用join(" AND ")
在每个条件之间连接AND
:
var yo = $('.subCat span[data-value].checked').map(function() {
return $(this).attr('data-value')+'=1';
}).get();
console.log(query + yo.join(" AND "));
This将为您提供:
SELECT * FROM table WHERE 30000_doublefrotements=1 AND contemporain=1 AND transitionnel=1
关于javascript - 从jquery数据attr值进行查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12166422/