1

我可以得到很好的结果,但是每个数据值结果都用逗号分隔,因为它是一个数组。我想在没有任何逗号的情况下进行查询,并可能删除最后一个 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">&#9675;&nbsp;Tous</span><br>
            <div class="after">
                <span data-value="30000_doublefrotements" class="checked">&#9675;&nbsp;+30,000 double frottement</span><br>
                <span data-value="atcc96_lavablecommercial">&#9675;&nbsp;ATTCC 96</span><br>
                <span data-value="nfpa701_inifuge">&#9675;&nbsp;NFPA 701</span><br>
                <span data-value="resistanrayonsuv">&#9675;&nbsp;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">&#9675;&nbsp;Tous</span><br>
            <div class="after">
                <span data-value="contemporain" class="checked">&#9675;&nbsp;Contemporain</span><br>
                <span data-value="traditionnel">&#9675;&nbsp;Traditionel</span><br>
                <span data-value="transitionnel" class="checked">&#9675;&nbsp;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 
4

3 回答 3

3

用于在每个条件之间join(" AND ")连接s:AND

var yo = $('.subCat span[data-value].checked').map(function() {
  return $(this).attr('data-value')+'=1';
}).get();
console.log(query + yo.join(" AND "));

会给你:

SELECT * FROM table WHERE 30000_doublefrotements=1 AND contemporain=1 AND transitionnel=1

于 2012-08-28T19:49:00.603 回答
0

试试这个:

//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();
    yo = yo.join(' ').replace(/\s*AND\s*$/, '');

    console.log(query+' '+yo);
});​
于 2012-08-28T19:49:15.890 回答
0

这对我有用:

//Submit Adv Search
$('#submitAdvSearch').click(function(){
    var query = "SELECT * FROM table WHERE ";
    var where_clause = [];
    var yo = $('.subCat span[data-value].checked').map(function(){
        var item = $(this).attr('data-value')+'=1';
        where_clause.push(item);           
    }).get();

    console.log(query+' '+ where_clause.join(" AND "));
});
于 2012-08-28T19:56:55.120 回答