select onchange 标签没有 运行 我的 Javascript 功能
select onchange tag does not run my Javascript function
我有以下代码:
JS:
function updateLink()
{
var selType = document.getElementByID("Types");
var selLen = document.getElementByID("Length");
var selLoc = document.getElementByID("Location");
var selMan = document.getElementByID("Manufacturer");
var test = document.getElementByID("test");
test.innerHTML="Test";
var selectedType = (selType.options[selType.selectedIndex].text);
var selectedLength = (selLen.options[selLen.selectedIndex].text);
var selectedLocation = (selLoc.options[selLoc.selectedIndex].text);
var selectedManufacturer = (selMan.options[selMan.selectedIndex].text);
document.getElementById("apply").href="myURL?typeName="+selectedType+"length="+selectedLength+"location="+selectedLocation+"manufacturer="+selectedManufacturer;
}
</script>
和HTML
<div id="filterdiv" class="dark">
<center><h3 id="test">Filters</h3></center>
<br>
<center>Type</center>
<select id="Types" onchange="updateLink()">
<option>All</option>
{% for types in TypesList %}
<option>{{types}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Inspection Period</center>
<select id="Inspection Period">
<option>All</option>
{% for inspections in InspectionList %}
<option>{{inspections}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Length</center>
<select id="Length">
<option>All</option>
{% for lengths in LengthList %}
<option>{{lengths}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Location</center>
<select id="Location">
<option>All</option>
{% for locations in LocationList %}
<option>{{locations}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Manufacturer</center>
<select id="Manufacturer">
<option>All</option>
{% for manufacturers in ManufacturerList %}
<option>{{manufacturers}}</option>
{%endfor%}
</select>
<br>
<br>
<a href="" id="apply"><button onclick="updatelist()">Apply Filter (TODO)</button></a>
<a href="http://myURL"><button>Reset Filters</button></a>
</div>
这是一个包含 select 个框的列表,其中包含从我的 Django 模型中获取的选项,用于在另一个 div 中过滤列表。按下我的按钮后,我被重定向到相同的 URL,没有附加参数。而且我更改标题的测试字符串也没有任何改变。所以我怀疑我的 JS 代码永远不会被触发。有人可以帮我吗?
我知道我的 onchange 只适用于类型 select 框。此代码不完整。我正在使用类型框进行测试。
我的URL 是我目前托管的本地地址的替代品。
替换此片段:
<br>
<br>
<a href="" id="apply"><button onclick="updatelist()">Apply Filter (TODO)</button></a>
<a href="http://myURL"><button>Reset Filters</button></a>
这个:
<input type="button" value="Click me" onclick="updatelist()">
我注意到为我回答此问题的用户已删除他们的评论。答案是一个简单的语法错误。对于 GetElementById,我将 ID 中的两个字母都大写了。语法和区分大小写的课程!
我有以下代码:
JS:
function updateLink()
{
var selType = document.getElementByID("Types");
var selLen = document.getElementByID("Length");
var selLoc = document.getElementByID("Location");
var selMan = document.getElementByID("Manufacturer");
var test = document.getElementByID("test");
test.innerHTML="Test";
var selectedType = (selType.options[selType.selectedIndex].text);
var selectedLength = (selLen.options[selLen.selectedIndex].text);
var selectedLocation = (selLoc.options[selLoc.selectedIndex].text);
var selectedManufacturer = (selMan.options[selMan.selectedIndex].text);
document.getElementById("apply").href="myURL?typeName="+selectedType+"length="+selectedLength+"location="+selectedLocation+"manufacturer="+selectedManufacturer;
}
</script>
和HTML
<div id="filterdiv" class="dark">
<center><h3 id="test">Filters</h3></center>
<br>
<center>Type</center>
<select id="Types" onchange="updateLink()">
<option>All</option>
{% for types in TypesList %}
<option>{{types}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Inspection Period</center>
<select id="Inspection Period">
<option>All</option>
{% for inspections in InspectionList %}
<option>{{inspections}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Length</center>
<select id="Length">
<option>All</option>
{% for lengths in LengthList %}
<option>{{lengths}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Location</center>
<select id="Location">
<option>All</option>
{% for locations in LocationList %}
<option>{{locations}}</option>
{%endfor%}
</select>
<br>
<br>
<center>Manufacturer</center>
<select id="Manufacturer">
<option>All</option>
{% for manufacturers in ManufacturerList %}
<option>{{manufacturers}}</option>
{%endfor%}
</select>
<br>
<br>
<a href="" id="apply"><button onclick="updatelist()">Apply Filter (TODO)</button></a>
<a href="http://myURL"><button>Reset Filters</button></a>
</div>
这是一个包含 select 个框的列表,其中包含从我的 Django 模型中获取的选项,用于在另一个 div 中过滤列表。按下我的按钮后,我被重定向到相同的 URL,没有附加参数。而且我更改标题的测试字符串也没有任何改变。所以我怀疑我的 JS 代码永远不会被触发。有人可以帮我吗?
我知道我的 onchange 只适用于类型 select 框。此代码不完整。我正在使用类型框进行测试。
我的URL 是我目前托管的本地地址的替代品。
替换此片段:
<br>
<br>
<a href="" id="apply"><button onclick="updatelist()">Apply Filter (TODO)</button></a>
<a href="http://myURL"><button>Reset Filters</button></a>
这个:
<input type="button" value="Click me" onclick="updatelist()">
我注意到为我回答此问题的用户已删除他们的评论。答案是一个简单的语法错误。对于 GetElementById,我将 ID 中的两个字母都大写了。语法和区分大小写的课程!