我在制作网站时遇到问题,当我选择特定选项时,它应该将我重定向到另一个索引 例如,当我选择“ Membrebénevole”时,将加载页面“ reg-benevole.html”。 当我选择“负责任的关联”时,将加载“ reg-assocation.html”页面。
`<div class="form-group">
<select id="cat" style="width:80%;" class="form-control" name="option">
<option value="Responsable association" onChange="catgValue =this.value;"> Responsable
association </option>
<option value="Membre bénevole" onChange="catgValue =this.value;">Membre bénevole
</option>
</select>
</div>
<div class="form-group">
<input type="submit" id="s" class="btn btn-primary" value="Soumettre">
</div>`
和Jquery代码:
$(document).on('click', '#s', function () {
if (catgValue = 'Membre bénevole') {
window.location.href = "reg-benevole.html";
}
else if (catgValue = 'Responsable association') {
window.location.href = "reg-assocation.html";
}
});
答案 0 :(得分:1)
第一个: 您正在使用单个“ =”进行比较。您需要使用“ ==”(或“ ===”),例如:
第二个: 您需要在选择输入内获得选择值,而不是在其外部
第三名: 如果您使用表格内的输入或将输入类型更改为e.preventDefault()
button
第四名: 要进行仔细检查,可以使用.trim()
来避免空格
$(document).on('click', '#s', function (e) {
e.preventDefault();
var catgValue = $('#cat').val().trim();
if (catgValue == 'Membre bénevole') {
window.location.href = "reg-benevole.html";
}
else if (catgValue == 'Responsable association') {
window.location.href = "reg-assocation.html";
}
});
答案 1 :(得分:0)
不仅要用单个=号进行设置,而且最好不要对选择进行change
事件,而不要进行click
事件。您不需要选项中的onchange事件。
$(document).on('change', '#s option', function () {
if (this.value == 'Membre bénevole') {
window.location.href = "reg-benevole.html";
}
else if (this.value == 'Responsable association') {
window.location.href = "reg-assocation.html";
}
});
答案 2 :(得分:0)
感谢您的回答!我太感激了,我改变了这样的代码,现在还没有..(顺便说一句,感谢您提醒我有关“ ==”的问题,我有点喝醉了xDD 而我用的是“单击”,因为它属于提交按钮而不是选择选项
$(document).on('click', '#s', function () {
if (catgValue == 'Membre bénevole') {
window.location.href = "reg-benevole.html";
}
else if (catgValue == 'Responsable association') {
window.location.href = "reg-assocation.html";
}
})