一個比較變態的js傳值,Query的bind、ajax閉包、上下文傳值

var getIDNameList = function (list, selected, text, btn, actionUrl, defaultKey, deleteKey, keyName, valueName, callBackFuc) 
{
    btn.bind("click keyup", { sText: text }, function (e) {
        $.ajax({
            url: actionUrl,
            contentType: "application/json",
            type: "GET",
            success: $.proxy(function (data) {
                var comparStr = this.val();
                for (var i = 0; i < data.length; i++) {
                    
                    if (data[i][valueNameStr].indexOf(comparStr) == -1)
                        continue;
                }
            }, e.data.sText)
        });
    });
}

//調用
var i=1;
getIDNameList(
             $('#List' + i),
             $('#S' + i),
             $('#searchText' + i),
             $('#searchBtn' + i),
             "../scripts/A04/A0410.json",
             null, null, "Key", "Value", function (obj) {
                 
             });
             
//HTML
<div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <span data-bind="attr:{id:'S'+$index()}">請選擇</span><span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu" data-bind="attr:{id:'List'+$index()}" ></ul>
    <div class="input-group">
        <input type="text" class="form-control" data-bind="attr:{id:'searchText'+$index()}" placeholder="專業類別名稱">
        <span class="input-group-btn">
            <button class="btn btn-primary" id="c2-searchBtn"  data-bind="attr:{id:'searchBtn'+$index()}"  type="button">
                搜索
            </button>
        </span>
    </div>
</div>
// A0410.json
[{
    "Key": "1",
    "Value": "哲學"
},
{
    "Key": "101",
    "Value": "哲學類"
},
{
    "Key": "10101",
    "Value": "哲學"
},
{
    "Key": "10102",
    "Value": "邏輯學"
}]
相關文章
相關標籤/搜索