Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类型错误:x未定义_Javascript_Html - Fatal编程技术网

Javascript 类型错误:x未定义

Javascript 类型错误:x未定义,javascript,html,Javascript,Html,我有这个标记 <form name="sortbyformtop"> <select onchange="location.href=sortbyformtop.sortbyselecttop.options[selectedIndex].value" id="sortbyselecttop"> <option value="/Search"></option> <option value="/Search?so

我有这个标记

<form name="sortbyformtop">
   <select onchange="location.href=sortbyformtop.sortbyselecttop.options[selectedIndex].value" id="sortbyselecttop">
      <option value="/Search"></option>
      <option value="/Search?sortby=accommodationtype">Accommodation type</option>
      ...
   </select>
</form>
这种事情在某种程度上可能发生吗?

试试看

location.href= document.getElementById('sortbyselecttop').options[document.getElementById('sortbyselecttop').selectedIndex].value;
或者很快

location.href= this.options[this.selectedIndex].value;
因为在该上下文中,这相当于document.getElementById'sortbyselecttop'

selectedIndex需要绑定到特定的select元素—它是select本身的属性,否则您将查找名为selectedIndex的全局未定义变量

location.href= document.getElementById('sortbyselecttop').options[document.getElementById('sortbyselecttop').selectedIndex].value;
或者很快

location.href= this.options[this.selectedIndex].value;
因为在该上下文中,这相当于document.getElementById'sortbyselecttop'

selectedIndex需要绑定到特定的select元素它是select本身的属性,否则您将查找名为selectedIndex的全局未定义变量,而不是sortbyformtop.sortbyselecttop。请提供此或文档。getElementById'sortbyselecttop'

代替sortbyformtop.sortbyselecttop,提供此或文档.getElementById'sortbyselecttop'


为什么要在内联事件处理程序中执行此操作

我相信这是有可能做到的-我想你可能需要一个'name'属性在你的select元素上,而不是一个ID,但是可能更容易有一个单独的函数给你更多的灵活性-然后从你的事件处理程序调用函数

onchange="doSomething();"

为什么要在内联事件处理程序中执行此操作

我相信这是有可能做到的-我想你可能需要一个'name'属性在你的select元素上,而不是一个ID,但是可能更容易有一个单独的函数给你更多的灵活性-然后从你的事件处理程序调用函数

onchange="doSomething();"
较短的版本:

location.href = this.options[this.selectedIndex].value
较短的版本:

location.href = this.options[this.selectedIndex].value

sortbyformtop的价值是什么?我们可能需要更多的代码,这里它实际上是未定义的……如果您使用一些库(如jQuery)来处理所有DOM混乱,您的生活会变得更加轻松,除非您真正体验过它的所有怪癖和烦恼。如果您担心使用库的任何缺点,请阅读。sortbyformtop的价值是什么?我们可能需要更多代码,在这里,它确实是未定义的……如果您使用jQuery之类的库来处理所有DOM混乱,您的生活会变得更加轻松,除非您真正体验过它的所有怪癖和烦恼。如果您担心使用库的任何负面影响,请阅读.I get TypeError:document.GetElementById或BySelectTop页面中有多少个具有该id的元素?如果只有一个sortbyselecttop,那么代码应该可以正常工作。或者尝试my updatewell,如果您使用此版本,您可以计算完全删除这些idI get TypeError:document.GetElementByIDs或BySelectTop页面中有多少个具有该id的元素?如果只有一个sortbyselecttop,那么代码应该可以正常工作。或者试试我的updatewell,如果你使用这个版本,你可以计算完全删除那些id