最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use array in autocomplete textbox jquery? - Stack Overflow

programmeradmin2浏览0评论

For Example

<link rel="stylesheet" href="//code.jquery/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery/jquery-1.10.2.js"></script>
<script src="//code.jquery/ui/1.11.4/jquery-ui.js"></script>

<script>
    $(function() { 
        var availableTags = [ "ActionScript", "AppleScript", "Asp"];
        var availableTagsCode = ["1", "2", "3"]; 
        $( "#tags" ).autoplete({ source: availableTags });
    });
</script>

<input id="tags" name="name">
<input id="tags_code" name="code">

Actually, I have tried to change code while select suggestion using following code:

$("#tags_code").val(availableTagsCode); 

I need to select suggestion test if i will select 0th array from tag the 0th code should assign to name="code" textbox.
Please help me to resolve this problem.

For Example

<link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery./jquery-1.10.2.js"></script>
<script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>

<script>
    $(function() { 
        var availableTags = [ "ActionScript", "AppleScript", "Asp"];
        var availableTagsCode = ["1", "2", "3"]; 
        $( "#tags" ).autoplete({ source: availableTags });
    });
</script>

<input id="tags" name="name">
<input id="tags_code" name="code">

Actually, I have tried to change code while select suggestion using following code:

$("#tags_code").val(availableTagsCode); 

I need to select suggestion test if i will select 0th array from tag the 0th code should assign to name="code" textbox.
Please help me to resolve this problem.

Share Improve this question edited Oct 30, 2015 at 9:11 Yeldar Kurmangaliyev 34.3k13 gold badges64 silver badges104 bronze badges asked Oct 30, 2015 at 6:20 Dinesh GDinesh G 1,3655 gold badges17 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Set select event handler

$(function() {
  var availableTags = ["ActionScript", "AppleScript", "Asp"];
  var availableTagsCode = ["1", "2", "3"];
  $("#tags").autoplete({
    source: availableTags,
    select: function(e, ui) {
      $('#tags_code').val(availableTagsCode[availableTags.indexOf(ui.item.value)]);
    }
  });
});
<link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery./jquery-1.10.2.js"></script>
<script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
<input id="tags" name="name">
<input id="tags_code" name="code">

You can define a select event handler:

$( "#tags" ).autoplete({ 
    source: availableTags,
    select: function(event, ui) {
        var index = availableTags.indexOf(ui.item.value);
        $("#tags_code").val(availableTagsCode[index]);  
    }
});

Here is the working JSFiddle demo.

Actually, jQuery UI allows you to use an array of objects with value and label properties when you provide source.
So, something like this will work and look better:

var tags = [
    {"label": "ActionScript", "value": 1},
    {"label": "AppleScript", "value": 2},
    {"label": "Asp", "value": 3}
];

$( "#tags" ).autoplete({ 
    source: tags,
    select: function (event, ui) {
        $("#tags_code").val(ui.item.value);  
    }
});

JSFiddle demo for the second approach.

If you don't want the value to be replaced after choosing, you can use custom property instead of value:

var tags = [
    {"label": "ActionScript", "code": 1},
    {"label": "AppleScript", "code": 2},
    {"label": "Asp", "code": 3}
];

$( "#tags" ).autoplete({ 
    source: tags,
    select: function (event, ui) {
        $("#tags_code").val(ui.item.code);  
    }
});

JSFiddle demo.

<script type="text/javascript">  
    $(document).ready(function () {
        $("#textBoxID").autoplete({
            source: function (request, responce) {
                $.ajax({
                    url: "webservice_Name.asmx/webservice_functionName",
                    method: "post",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({ parameterName: request.term }),
                    dataType: 'json',
                    success: function (data) {

                        responce(data.d);
                        if (data.d.length > 0)
                            $('#spnError').text("");
                        else
                            $('#spnError').text("Not Matching Any Result");
                        console.log(data.d.length);
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {
                console.log("You selected: " + ui.item.label);
                $.ajax({
                    url: "webservice_Name.asmx/webservice_functionNameForgetID",
                    method: "post",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({ parameterName: ui.item.label }),
                    dataType: 'json',
                    success: function (data) {
                        console.log(data.d);

                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            }
        });
    });
</script>
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>