如何用单引号在javascript中存储数组?可能吗

时间:2014-09-26 06:49:46

标签: javascript jquery

我正在尝试通过javascript创建一个用于过滤产品的查询。 SELECT * FROM TBN将在下一页。使用javascript jquery生成查询的其余部分

工作小提琴:http://jsfiddle.net/fo9cct34/

它将根据复选框勾选创建查询

    AND brand=Property 1 OR brand=Property 2 OR brand=Property 3
    AND price > Color 1 OR price > Color 2
    AND camera = Location 1 OR camera = Location 2

但是现在我需要添加一个“#”; '像这样引用

AND brand='Property 1' OR brand='Property 2' OR brand='Property 3'
AND price > 'Color 1' OR price > 'Color 2'
AND camera = 'Location 1' OR camera = 'Location 2'
请某人帮助我。谢谢

6 个答案:

答案 0 :(得分:1)

将它与单引号连接起来

if (this.checked) byBrand.push("'"+$(this).attr("value")+"'")

答案 1 :(得分:1)

您可以在代码中尝试此操作:"'" + this.value + "'"

答案 2 :(得分:1)

您只需在其值之前和之后放置单引号:

 $("input").on( "change", function() {
        var str = "";
        if (byBrand.length) str += "    AND brand='" +  byBrand.join(" OR brand='") + "'\n";
        if (byPrice.length) str += (str == "\n" ? "    ":"    AND price > '") + "" +  byPrice.join(" OR price > '") + "'\n";
        if (byCamera.length) str += (str == " \n" ? "    ":"    AND camera") + " ='" +  byCamera.join(" OR camera ='") + "'\n";

        $("#result").html(str);

    });

<强> See the Updated Fiddle Here

答案 3 :(得分:1)

还有更多解决方案。

  1. 加入数组时添加引号:
  2. (str == "\n" ? " ":" AND price > '") + "" + byPrice.join('\' OR price > \'') + "'\n";

    1. 使用引号存储值:

      byCamera.push("'" + $(this).attr("value") + "'");

      removeA(byCamera, "'" + $(this).attr("value") + "'");

答案 4 :(得分:0)

试试这个

$("input[name=brand]").on( "change", function() {
    var v = $(this).attr("value");
    var v = '\''+v+'\''
    if (this.checked) byBrand.push(v);
    else removeA(byBrand, v);
});

答案 5 :(得分:0)

var Products = [];
    $().ready(function () {
        Products.push({ ProductID: '1', ProductName: 'ABC', ProductCategory: 'XYZ' });
        Products.push({ ProductID: '2', ProductName: 'DEF', ProductCategory: 'XYZ' });
        Products.push({ ProductID: '3', ProductName: 'GHI', ProductCategory: 'XYZ' });

        alert('Product #: ' + Products[2].ProductID + '\n' + 'Product Name: ' + Products[2].ProductName + '\n' + 'Product Category: ' + Products[2].ProductCategory)
    });

它会帮助你