Javascript 如何在PHP中获取文本框ID

Javascript 如何在PHP中获取文本框ID,javascript,jquery,Javascript,Jquery,我创建了一个文本框 <input type="text" onkeyup="lookup(this.value);" value="" name="Name_Code" id="Name_Code"> 试着使用这可能是它的作品 onkeyup="lookup(this.id);" function lookup(id){ alert(id); } 您需要将事件传递给您的函数,它就在那里 首先将其添加到输入标记: onkeyup="lookup(event, this.v

我创建了一个文本框

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

试着使用这可能是它的作品

onkeyup="lookup(this.id);"

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

您需要将
事件
传递给您的函数,它就在那里

首先将其添加到输入标记:

onkeyup="lookup(event, this.value);"
现在的功能是:

function lookup(event, inputString) {
   ...
   var sender = event.target || event.srcElement;
   var currentId = sender.id;
   ...
}
无论如何,您误用了jQuery。正确的用法是:

$(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);
       }
   });
});

测试用例在这里可用:

如果使用jquery,应该使用jquery绑定

对于您现有的设置,这将起作用

<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);
    }
};

函数查找(obj){
如果(obj.value.length==0){
//隐藏建议框。
$(“#建议”).hide();
}否则{
var currentId=obj.id;//注意我传递了-this-而不是-this.value-
警报(当前ID);
}
};
既然您已经有了jquery,我建议您应该这样做

<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);
    }
};

$(“#名称"代码”).keyup(查找);
函数查找(){
$this=$(this);//因为我们有jquery对象
如果($this.val().length==0){
//隐藏建议框。
$(“#建议”).hide();
}否则{
var currentId=$this.attr('id');//注意,我传递了-this-而不是-this.value-
警报(当前ID);
}
};

$(此)
在您的案例中不起作用-它没有上下文。你想要什么身份证?我们需要更多的细节和更多的代码在这里。另外,请回到你以前的问题,接受正确答案或你最终使用的答案。我真的不理解你的问题。您提供HTML和JavaScript代码,但要求使用PHP。你可以描述一下你试图解决的实际问题吗?难怪它不起作用。局部变量$(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);
    }
};