2012-02-10 3 views
2

전화를 걸었지만 아무 것도 반환하지 않는 것 같습니다. 내가 잘못하고있는 것입니까? 당신이 객체에게Jquery Tokeninput ID 대신 이름을 얻는 방법

selector.tokenInput("get"); 

이 각 항목 종류의 객체 인합니다 (tokeninput에서 선택 토큰의 배열을 취득하는 방법입니다 {ID를 얻을 수 tokenInput 자신의 방법을 사용하는 이유하지

 <script type="text/javascript"> 
      $(document).ready(function() { 
       $("input[type=button]").click(function() { 
        alert("Would submit: " + $(this).siblings("input[type=text]").val()); 
       }); 
      }); 


    $(document).ready(function() { 
    $("#button1").click(function() { 


        var abc = $('#demo-input-local').tokenInput("get"); 
        alert(abc); 
        alert(abc[7].name); 

     var inputval =$(this).siblings("input[type=text]").val(); 
     alert("inputval " +inputval); 

alert(abc[inputval].name); 

     $("#some-text").focusEnd(); 

     $('#some-text').val($('#some-text').val()+' ' + inputval); 

    }); 
    }); 

      </script> 

    <label> Enter Phone or Email or http: </label><input type="text" id="demo-input-local" name="blah" /><input type="button" id="button1" value="Submit" /> 
             <script type="text/javascript"> 
              $(document).ready(function() { 
               $("#demo-input-local").tokenInput([ 
               { id: 7, name: "[email protected]", "value": "@" , "prefix": "Email1"}, 
               { id: 11, name: "[email protected]", "value": "@" ,"prefix": "Email2"}, 
               { id: 13, name: "[email protected]", "value": "@" ,"prefix": "Email3"}, 
               { id: 17, name: "[email protected]", "value": "@", "prefix": "Email4" }, 
               { id: 19, name: "[email protected]", "value": "@", "prefix": "Email5" }, 
               { id: 23, name: "www.C#.com", "value": "http", "prefix": "Website1" }, 
               { id: 29, name: "www.C.com", "value": "http", "prefix": "Website2" }, 
               { id: 31, name: "www.Fortran.com", "value": "http", "prefix": "Website3" }, 
               { id: 37, name: "www.VisualBasic.com", "value": "http", "prefix": "Website4" }, 
               { id: 41, name: "www.DoC.com", "value": "http", "prefix": "Website5" }, 
               { id: 43, name: "www.C++.com", "value": "http" , "prefix": "Website6"}, 
               { id: 47, name: "www.Java.com", "value": "http" , "prefix": "Website7"}, 
               { id: 37, name: "111-111-1111", "value": "#", "prefix": "Phone1" }, 
               { id: 41, name: "222-222-2222", "value": "#", "prefix": "Phone2" }, 
               { id: 43, name: "333-333-3333", "value": "#" , "prefix": "Phone3"}, 
               { id: 47, name: "555-555-5555", "value": "#" , "prefix": "Phone4"} 


              ], { 
               theme: "facebook", 

               propertyToSearch: "value", 
               hintText: "Type '@' for email or 'http' for website or '#' for phone", 
               tokenFormatter: function (item) { return "<li><p>" + item.prefix + "</p></li>" }, 
               resultsFormatter: function (item) { return "<li>" + item.prefix +" "+ item.name + "</li>" } 
              }); 
              }); 
             </script> 
            </div> 
+0

당신은 완전히 원래의 질문에 user244394을 통해 편집 한! –

답변

4

또한,이 시도 실제로 제출 된 값은 쉼표로 구분 된 ID 목록입니다.

위의 것은 매우 익숙하지 않은 접근법입니다. 마찬가지로 ABC가 가리키는 .tokenInput("get"); 메서드에 의해 반환 된 배열을 반복 할 수 있습니다. ABC의 말씀 가정에

.tokenInput("get");이 작동합니다 다음 올바른지 :

자바 스크립트 :

$("input[type=button]").click(function() { 
    var arr = $(this).siblings("input[type=text]").tokenInput("get");//Returns (we think) an array of objects 
    var names = []; 
    $.each(arr, function(i, obj){ 
     names.push(obj.name);//build an array of just the names 
    }); 
    alert("Names are: " + names.join()); 
}); 
+0

감사합니다 내 코드를 봅니다 .tokenInput ("get"); 하지만 아무것도 반환하지 않았다. var abc = selector.tokenInput ("get"); abc [0] .id; // 첫 번째 배열 요소에 대해 abc [0] .name ami missing something? – user244394

+0

ABC가 말한 것을 바탕으로, 위의 대답에'.tokenInput ("get");에 의해 반환 된 배열을 통해 루프를 더 추가했습니다. –

3

친애하는 : x, 이름 : y}). 지금까지의 내가의를 말할 수있는,

$(document).ready(function() { 
    $("input[type=button]").click(function() { 
     var names = []; 
     $(this).siblings("ul").find('li p').each(function(){ 
      names.push($(this).html()); 
      //names.push($(this).text());//possibly better 
     }); 
     alert("Names are: " + names.join()); 
    }); 
}); 

을하지만 :

자바 스크립트 : 당신은 그냥 이름의 쉼표로 구분 된 목록을 경고하려는 경우

var abc = selector.tokenInput("get"); 
abc[0].id; // for first array element 
abc[0].name 
+0

내 코드를 업데이트했습니다. abc [0]을 호출 할 때 vale를 반환하지 않는 것 같습니다. 누락 된 것이 있습니까? 위에 코드를 게시했습니다. 감사합니다. – user244394

+0

당신이 더 잘 http://loopj.com/jquery-tokeninput/ tokenInput documentation을 참조하십시오. –

+0

ABC가 말한 것을 기반으로, 배열을 통해 루프가 반환 된 내 대답에 코드가 추가되었습니다. .tokenInput ("get");'에 의해. –

관련 문제