我不明白,如何根据选择的选项值为同一个按钮(以联系表形式)建立不同的链接。
<select name="level" class="level form-control" id="level" required>
<option value="1">A1</option>
<option value="2">A2</option>
<option value="3">B1</option>
</select>
(function ($) {
jQuery(document).ready(($) => {
$(document).on('submit', '#contact_form_submit', (e) => {
e.preventDefault();
const name = $('#name').val();
const phone = $('#phone').val();
const level = $('#level').val();
const email = $('#email').val();
if (name && email) {
$.ajax({
type: 'POST',
url: 'contact.php',
data: {
name,
phone,
level,
email,
},
success(data) {
$('#contact_form_submit').children('.email-success').remove();
$('#contact_form_submit').prepend(`<span class="alert alert-success email-success">${data}</span>`);
$('#name').val('');
$('#phone').val('');
$('#level').val('');
$('#email').val('');
location.href = "https://www.qwerty.com";
// $('#map').height('576px');
$('.email-success').fadeOut(3000);
},
error(res) {
},
});
} else {
$('#contact_form_submit').children('.email-success').remove();
$('#contact_form_submit').prepend('<span class="alert alert-danger email-success">All Fields are Required.</span>');
// $('#map').height('576px');
$('.email-success').fadeOut(3000);
}
});
});
}(jQuery));
因此,您可以看到,现在,如果用户输入正确的数据,他将被重定向到某个链接,但是我必须为不同的选项放置不同的链接。我试图玩'if',但没有成功。谢谢。
您最好还添加您尝试过但无法正常工作的代码。因此,我们可以检查它是否存在错别字或误解。简单的IF应该适合您。
if(level == '1'){
location.href = "https://www.qwerty.com";
}
if(level == '2'){
location.href = "https://www.google.com";
}
if(level == '3'){
location.href = "https://www.yahoo.com";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句