如何在单个选择的jquery上隐藏搜索?

时间:2015-09-28 04:04:23

标签: javascript jquery html5 select jquery-chosen

我已经在http://harvesthq.github.io/chosen/选择了jquery。好的,我想"隐藏单一选择的搜索"同样的例子(例如guthub /上面选择的例子),例如FLAG=false隐藏它们。我该怎么做?



var data = [{
    "BU_ID": "B01",
        "BU_NAME": "Agro Feed",
        "BU_DES": "Agro Feed",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}, {
    "BU_ID": "B02",
        "BU_NAME": "Agro Farm",
        "BU_DES": "Agro Farm",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": false
},{
    "BU_ID": "B03",
        "BU_NAME": "Agro Food",
        "BU_DES": "Agro Food",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}
           ];

$("#cb_info").empty();
$.each(data, function (idx, obj) {
    $("#cb_info").append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
}); 
$("#cb_info").chosen({ width: "auto" });
$("#cb_info").trigger("chosen:updated");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.jquery.js"></script>
<select id="cb_info"></select>
&#13;
&#13;
&#13;

谢谢你们。

2 个答案:

答案 0 :(得分:1)

var data = [{
    "BU_ID": "B01",
        "BU_NAME": "Agro Feed",
        "BU_DES": "Agro Feed",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}, {
    "BU_ID": "B02",
        "BU_NAME": "Agro Farm",
        "BU_DES": "Agro Farm",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": false
}, {
    "BU_ID": "B03",
        "BU_NAME": "Agro Food",
        "BU_DES": "Agro Food",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}];

$("#cb_info").empty();
$.each(data, function (idx, obj) {
    if (obj.FLAG === true) {
        $("#cb_info").append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
    }
});

只需添加条件if (obj.FLAG === true) { 只有带有true的标志才会被添加到列表中,因此您不需要隐藏任何内容

DEMO

<强>已更新

demo

答案 1 :(得分:1)

你的问题不是那么清楚。但是,如果你想要的是禁用带有FLAG的项:false,那么只需为这些选项指定disabled属性。

&#13;
&#13;
var data = [{
    "BU_ID": "B01",
        "BU_NAME": "Agro Feed",
        "BU_DES": "Agro Feed",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}, {
    "BU_ID": "B02",
        "BU_NAME": "Agro Farm",
        "BU_DES": "Agro Farm",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": false
},{
    "BU_ID": "B03",
        "BU_NAME": "Agro Food",
        "BU_DES": "Agro Food",
        "EDIT_DATE": "2015-05-05T00:00:00",
        "EDIT_BY": "",
        "FLAG": true
}
           ];

$("#cb_info").empty();
$.each(data, function (idx, obj) {
var status = (!obj.FLAG)?'disabled':'';
    $("#cb_info").append('<option '+status+' value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
}); 
$("#cb_info").chosen({ width: "auto" });
$("#cb_info").trigger("chosen:updated");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.jquery.js"></script>
<select id="cb_info"></select>
&#13;
&#13;
&#13;