Javascript 将select的值添加到a href
我有两个选择框。我希望这些字段的输出更改链接的href attibuteJavascript 将select的值添加到a href,javascript,jquery,Javascript,Jquery,我有两个选择框。我希望这些字段的输出更改链接的href attibute <select class="option1"> <option>Selecteer onderwijssoort</option> <option value="basisonderwijs">Basisonderwijs</option> <option value="voortgezetonderwijs">Voortge
<select class="option1">
<option>Selecteer onderwijssoort</option>
<option value="basisonderwijs">Basisonderwijs</option>
<option value="voortgezetonderwijs">Voortgezet onderwijs</option>
<option value="nt2">NT2</option>
</select>
<select class="option2">
<option>Selecteer niveau</option>
<option value="vmbot">VMBO T</option>
<option value="havo">HAVO</option>
<option value="vwo">VWO</option>
</select>
<a href="/filter/#1=OPTION1&2=OPTION2" id="methodelink">Start vergelijking</a>
选择EER onderwijssoort
巴斯森德威斯酒店
Voortgezet onderwijs
NT2
选择者niveau
VMBO T
哈沃
VWO
下面是一个使用jQuery的解决方案:
var selectedOption1,selectedOption2=“”;
$(“.option1”).change(函数(){
选择选项1=$(this.val();
$(“#methodelink”).attr(“href”,“/filter/#1=“+selectedOption1+”&2=“+selectedOption2”);
});
$(“.option2”).change(函数(){
selectedOption2=$(this.val();
$(“#methodelink”).attr(“href”,“/filter/#1=“+selectedOption1+”&2=“+selectedOption2”);
});代码>
选择EER onderwijssoort
巴斯森德威斯酒店
Voortgezet onderwijs
NT2
选择者niveau
VMBO T
哈沃
VWO
您可以使用vanilla JS实现这一点。
请看以下内容:
let opt1,opt2;
函数updateOpt(){
opt1=document.getElementsByCassName('option1')[0]。值;
opt2=document.getElementsByClassName('option2')[0]。值;
let href=“/filter/#1=“+opt1+”&2=“+opt2;
设a=document.getElementById(“methodelink”);
a、 setAttribute(“href”,href);
设p=document.getElementsByTagName(“p”)[0];
p、 innerHTML=a;
}
选择EER onderwijssoort
巴斯森德威斯酒店
Voortgezet onderwijs
NT2
选择者niveau
VMBO T
哈沃
VWO
欢迎来到堆栈溢出!请通读一遍,尤其是你在这里的最佳选择是做你的研究,对于相关的话题等等,并尝试一下。如果你在做了更多的研究和搜索后陷入困境,无法摆脱困境,请发布一份你的尝试,并明确指出你陷入困境的地方。人们会很乐意帮忙的。这能回答你的问题吗?另外,向我们展示您的尝试。另外,请看以下内容: