我正在我的网站上设置一个有2个下拉框的页面,第二个下拉框是从第一个下拉框中的选定选项中填充的。
我的代码工作得很好,但是我无法让go按钮在第二个下拉框中找到所选的值来调用URL。
如何从下拉框2中获得Find Course按钮以调用所选选项中的URL?
任何帮助都会很感激:)我是JavaScript和高级HTML的超级新手。
<div id="myQuestions">
<select id="QuestionOptions">
<option value="Aa">Choose a Faculty</option>
<option value="Ba">Faculty 1</option>
<option value="Ca">Faculty 2</option>
</select>
</div>
<div id="myAnswers">
<div id="Aa" style="display: Aa;">
<div id="F1">
<select id="F1answers">
<option value="A1">Course Selection</option>
</select>
</div>
</div>
<div id="Ba" style="display: none;">
<div id="F2">
<select id="F2answers">
<option value="URL 1 GOES HERE">Course 1</option>
<option value="URL 2 GOES HERE">Course 2</option>
</select>
</div>
</div>
<div id="Ca" style="display: none;">
<div id="F3">
<select id="F3answers">
<option value="URL 3 GOES HERE">Course 3</option>
</select>
</div>
</div>
</div>
<div class="FindCourse">
<a id="CourseGo" href="HOMEPAGE URL"> Find Course </a>
</div>JS
$(function () {
$('#QuestionOptions').change(function () {
$('#myAnswers > div').hide();
$('#myAnswers').find('#' + $(this).val()).show();
});
});
var sel = document.getElementById('myAnswers');
sel.onchange = function () {
document.getElementById("CourseGo").href = this.value;
}发布于 2019-08-01 16:50:28
您不应该对多个元素使用属性id,但是这个脚本将按照您的逻辑工作:
const $myAnswersDiv = $("#myAnswers > div");
const $courseGo = $("#CourseGo");
const $myAnswers = $("#myAnswers");
$(function() {
$("#QuestionOptions").change(function() {
$myAnswersDiv.hide();
$myAnswers.find("#" + $(this).val()).show();
// targeting (#myAnswers > div > select) without style="display: none"
let $selectVisible = $myAnswersDiv
.not('[style$="display: none;"]')
.find("select");
// setting the current value of select to <a id="CourseGo">
$courseGo.attr("href", $($selectVisible).val());
// listener on the current <select> to set value to <a id="CourseGo">
$selectVisible.change(function() {
$courseGo.attr("href", $(this).val());
});
});
});https://stackoverflow.com/questions/57311766
复制相似问题