我只是这样但是我从java脚本的下拉菜单中得不到任何价值来将它与字符串进行比较
// JavaScript Document
var singleMulticontainer = document.getElementById("singleMultiContainer");
var singleMultiValue = singleMultiContainer.options[singleMultiContainer.selectedIndex].value;
var nextButton = document.getElementById("nextButton");
var multipleSetWindow = "window.location='multiSet.html'"
if(singleMultiValue == "multi"){
document.getElementById("nextButton").setAttribute("data-location", "multiSet.html");
}
else{
alert("notworking");
}
下拉菜单就是这个:如何从单个或多个容器的选项中获取值?
<form id"suspendedProperties">
<p><h4>Select Station:
<select name="stationDropdown">
<option value="50028000">Tanama</option>
<option value="60008001">Example Riv1</option>
<option value="60008002">Example Riv2</option>
<option value="60008003">Example Riv3</option>
<option value="60008004">Example Riv4</option>
</select>
</h4></p>
<p>Select Sample Medium:
<select name="sampleMediumDropdown">
<option value="Wer">Wer</option>
<option value="WSQ">WSQ</option>
</select>
</p>
<p>Begin Date
<input type="date"/>
</p>
<p>Hydrologic Event: <select name="hydroEvent">
<option value="1">Example 1</option>
<option value="2">Example 2</option>
<option value="3">Example 3</option>
<option value="4">Example 4</option>
<option value="5">Example 5</option>
<option value="6">Example 6</option>
</select>
</p>
<p>Add:<input type="number" size=""/> <select name="singleMultiContainer"><option value="single">Single container sample</option><option value="multi">Multiple sets container</option></select></p>
<p>Analyses Requested:(Applies to all samples)<br/></p>
<div id="analyses" >
<input type="checkbox" name="analysis" value="C">Concentration</input>
<input type="checkbox" name="analysis" value="SF">Sand-fine break**</input>
<input type="checkbox" name="analysis" value="SA">Sand analysis**</input> <br/>
<input type="checkbox" name="analysis" value="T">Turbidity</input>
<input type="checkbox" name="analysis" value="LOI">Loss-on-ignition**</input>
<input type="checkbox" name="analysis" value="DS">Dissolve solids</input> <br/>
<input type="checkbox" name="analysis" value="SC">Specific conductance</input>
<input type="checkbox" name="analysis" value="Z">Full-size fractions**</input>
</div>
<input type="button" value="Back" onClick="window.location='SED_WE.html'"/>
<input type="button" value="Next" onClick="window.location='SampleInfo.html'"/>
</form>
答案 0 :(得分:2)
您忘记为自己的选择添加了ID。我添加了<select name="singleMultiContainer" id="singleMultiContainer">
虽然您在脚本中“寻找它”但没有ID
你也有一些拼写错误,我修复了我找到的错误,所以请检查这是否是你想要的。
HTLM
<form id="suspendedProperties">
<p>
<h4>Select Station:
<select name="stationDropdown">
<option value="50028000">Tanama</option>
<option value="60008001">Example Riv1</option>
<option value="60008002">Example Riv2</option>
<option value="60008003">Example Riv3</option>
<option value="60008004">Example Riv4</option>
</select>
</h4>
</p>
<p>Select Sample Medium:
<select name="sampleMediumDropdown">
<option value="Wer">Wer</option>
<option value="WSQ">WSQ</option>
</select>
</p>
<p>Begin Date
<input type="date" />
</p>
<p>Hydrologic Event:
<select name="hydroEvent">
<option value="1">Example 1</option>
<option value="2">Example 2</option>
<option value="3">Example 3</option>
<option value="4">Example 4</option>
<option value="5">Example 5</option>
<option value="6">Example 6</option>
</select>
</p>
<p>Add:
<input type="number" size="" />
<select name="singleMultiContainer" id="singleMultiContainer">
<option value="single">Single container sample</option>
<option value="multi">Multiple sets container</option>
</select>
</p>
<p>Analyses Requested:(Applies to all samples)
<br/>
</p>
<div id="analyses">
<input type="checkbox" name="analysis" value="C">Concentration</input>
<input type="checkbox" name="analysis" value="SF">Sand-fine break**</input>
<input type="checkbox" name="analysis" value="SA">Sand analysis**</input>
<br/>
<input type="checkbox" name="analysis" value="T">Turbidity</input>
<input type="checkbox" name="analysis" value="LOI">Loss-on-ignition**</input>
<input type="checkbox" name="analysis" value="DS">Dissolve solids</input>
<br/>
<input type="checkbox" name="analysis" value="SC">Specific conductance</input>
<input type="checkbox" name="analysis" value="Z">Full-size fractions**</input>
</div>
<input type="button" value="Back" onClick="window.location='SED_WE.html'" />
<input type="button" value="Next" id="nextButton" onClick="window.location='SampleInfo.html'" />
</form>
JS
// JavaScript Document
var singleMultiContainer = document.getElementById("singleMultiContainer");
var singleMultiValue = singleMultiContainer.options[singleMultiContainer.selectedIndex].value;
var nextButton = document.getElementById("nextButton");
var multipleSetWindow = "window.location='multiSet.html'"
if (singleMultiValue == "multi") {
document.getElementById("nextButton").setAttribute("onclick", "window.location='multiSet.html'");
} else {
alert("notworking");
}