开发者

How can I get a text box ID in PHP

I created a text box

<input type="text" onkeyup="lookup(this.value);" value="" name="Name_Code" id="Name_Code">

In the lookup function, I need to eveluate the control ID. How I can get this ID without knowing the exact ID?开发者_如何学C

I tried this, but it is not working:

function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        var currentId = $(this).attr('id');
        alert(currentId);
    }
};


You need to pass the event to your function and it's in there.

First add this to your input tag:

onkeyup="lookup(event, this.value);"

And the function will now be:

function lookup(event, inputString) {
   ...
   var sender = event.target || event.srcElement;
   var currentId = sender.id;
   ...
}

Anyhow, you're misusing jQuery. Correct usage will be:

$(document).ready(function() {
   $("#Name_Code").bind("keyup", function() {
      var inputString = $(this).val();
      if(inputString.length == 0) {
           // Hide the suggestion box.
           $('#suggestions').hide();
       } else {
          var currentId = $(this).attr('id');
           alert(currentId);
       }
   });
});

Test case is available here: http://jsfiddle.net/yahavbr/LpbW9/


You should be using jquery binding if you use jquery.

For your existing setup, this will work

<input type="text" onkeyup="lookup(this);" value="" name="Name_Code" id="Name_Code">

function lookup(obj) {
    if(obj.value.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        var currentId = obj.id; //note that I passed -this- and not -this.value-
        alert(currentId);
    }
};

Since you already have jquery I suggest you should do this

<input type="text" value="" name="Name_Code" id="Name_Code">

$('#Name_Code').keyup(lookup);

function lookup() {
    $this = $(this); //since we have the jquery object
    if($this.val().length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        var currentId = $this.attr('id'); //note that I passed -this- and not -this.value-
        alert(currentId);
    }
};


Try to use this may be its works

onkeyup="lookup(this.id);"

function lookup(id){
    alert(id);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜