Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[Jquery] Autocomplete change event and this

Pagina: 1
Acties:

  • easydisk
  • Registratie: Februari 2000
  • Laatst online: 23-11 10:34
Heb een hele set input text velden met id's als input001, input002, input003, etc allemaal hebben ze een autocomplete functie d.m.v. jquery.

Nu wil ik in de select / change functie het element id gebruiken.. helaas in deze functies is de variable $(this) een niet bestaande, ook $(this.element).attr('id') is undefined..

Nu maar opgelost met $(':focus').attr('id')... maar als ik het vakje leeg maak gaat hij de focus verliezen en werkt het niet..

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$(function(){
    $('.auto_wo').autocomplete({
        source: function(request, response) {
        $.ajax({
          url: "/url/ajax_file.php",
               dataType: "json",
          data: {
            term : request.term,
            asset : $(this.element).attr('id')
          },
          success: function(data) {
            response(data);
          }
        }); 
        } , 
        minLength: 0,
        delay: 0,
        select: function( event, ui) {
            if (ui.item != null) {
                showWOText( $(':focus').attr('id'), ui.item.label);
            }
        },
        change: function(event, ui) { 
            if (ui.item != null) {
                showWOText( $(':focus').attr('id'), ui.item.label);
            }
        }
    });
});


Hoe kan ik hier toch het element id doorgeven aan de functie showWOText ?

  • Matis
  • Registratie: Januari 2007
  • Laatst online: 20:57

Matis

Rubber Rocket

Je krijgt toch je ui mee, kun je daaruit geen veld elimineren?

Edit;
Gevonden, onder event.target kun je het DOM-object vinden dat het event getriggered heeft.
Testcase, want het is toch regenachtig weer: http://jsfiddle.net/vmBy2/

[ Voor 62% gewijzigd door Matis op 07-08-2013 19:02 ]

If money talks then I'm a mime
If time is money then I'm out of time


  • easydisk
  • Registratie: Februari 2000
  • Laatst online: 23-11 10:34
Dank u !

event.target.id :)

ik zat idd met ui.element te klooien, maar dat werkt niet meer in recente versies van jquery.

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 20-11 11:59

NMe

Quia Ego Sic Dico.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • R4gnax
  • Registratie: Maart 2009
  • Laatst online: 06-09 17:51
Vergeet niet dat je altijd nog de context kunt rebinden:

JavaScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$( function() {

  function source( request, response ) {  
    $
      .ajax({
        url      : "/url/ajax_file.php",
        dataType : "json",
        data     : { term : request.term, asset : this.id },        
      })
      .done( response );
  }
    
  function update( event, ui ) {
    var asset = this.id;
    
    ui.item && showWOText( asset, ui.item.label );
  }

  $( ".auto_wo" ).each( function() {
    el.autocomplete({
      minLength : 0,
      delay     : 0,
      source    : $.proxy( source, this )
      select    : $.proxy( update, this )
      change    : $.proxy( update, this )
    }
  });
  
});


Misschien wat netter? Weet niet waar je het element mss. nog meer voor nodig hebt.

[ Voor 8% gewijzigd door R4gnax op 07-08-2013 22:09 ]