Javascript Typeerror:仅在FireFox中未定义表单元素
出于某种原因,我只在FireFox中遇到一个错误: Typeerror:document.forms.myCity.optionname未定义 该脚本适用于所有其他浏览器:Javascript Typeerror:仅在FireFox中未定义表单元素,javascript,firefox,Javascript,Firefox,出于某种原因,我只在FireFox中遇到一个错误: Typeerror:document.forms.myCity.optionname未定义 该脚本适用于所有其他浏览器: function WriteCookie() { document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; cookievalue = document.forms['myCity'].optionname.value +
function WriteCookie()
{
document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
cookievalue = document.forms['myCity'].optionname.value + ";";
document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
window.location.href = "http://mywebsite.com";
}
此脚本位于标题中,并通过以下表单执行:
<form name="myCity" action="http://mywebsite.com/" method="POST">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='". $optionname ."'><option selected='". $selected . "' value='" . $emptyvalue . "'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city');
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" value="click" name="submit" onclick="WriteCookie()">
</form>
您的错误是:
Typeerror:document.forms.myCity.optionname未定义
我认为问题在于这一点:
<form name="myCity" action="http://mywebsite.com/" method="POST">
您希望在其中同时包含name
和id
,以涵盖不同浏览器上的所有基础及其XHTML1.1标准的实现
但如果这仍然不起作用,只需在您的JavaScript中在id
更改的顶部执行此操作:
function WriteCookie()
{
document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
cookievalue = document.getElementById("myCity").optionname.value + ";";
document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
window.location.href = "http://mywebsite.com";
}
我把这行改成这样:
<form name="myCity" id="myCity" action="http://mywebsite.com/" method="POST">
cookievalue = document.forms['myCity'].optionname.value + ";";
为此:
cookievalue = document.getElementById("myCity").optionname.value + ";";
我猜你把打字错误的信息弄错了,对吧?(文档
,而不是文档
)