AJAX问题与value.length

Below is a very simple AJAX request that works just fine until I put a simple statement inside of it. It's failing because of line #4.

var wlen = str.value.length

Below is the AJAX code

<script type="text/javascript">
function name_search(str)
{
var wlen = str.value.length;
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("name_div").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/it/device/action/name_search_new.php?q="+str,true);
xmlhttp.send();
}
</script>

The name_search_new.php page looks up similar values to what is typed in the text box and returns those values. This page is irrelevant to this issue though because any php page will always fail with the line 4 new variable/assignment and it will always work with the variable/assignment gone.

If it helps, the request for this function is listed below... It also fails within a 'textarea' box too.

<input type="text" name="name" id="name" value="" style="color:red" 
placeholder="Device/Item(Required)" onkeyup="name_search(this.value)" />

Any help is appreciated, Jay

You are calling name_search(this.value); thus, str = this.value. Your line 4 then effectively executes this.value.value.length. However, this.value is a string, which doesn't have a property called value - so this.value.value is undefined. Accessing a property on undefined will fail.

Change to str.length and it should work.

BTW: the problem has nothing to do with AJAX.

You don't need to do str.value.length. str.length is what you need.

here is a jsfiddle demonstrating:

http://jsfiddle.net/ahEeH/