[英]Change selected tab based on selected radio button and option with jQuery
我要實現以下目標:
如果從選擇列表中選擇了德國(de)以外的國家,並且當前選擇了“打印”選項卡,則應選擇“數字”選項卡,頁面應向上滾動並顯示一個警告框。
到目前為止,我的代碼可以運行,但是第二個if循環的第二個條件未評估:
if (this.value! = 'de' && $("input [value = 'print']"). prop ("checked", true)) {
..
}
因此,每次從選擇列表中進行選擇時,選項卡都會更改為“數字”,並且頁面會向上滾動,盡管只有選擇了“打印”選項卡時才如此。
這是我完整的代碼:
// if a country other than germany is elected, set selected tab to digital and scroll to top
$("select[name='country']").change(function(e){
$("select[name='country'] option").each(function() {
if (this.selected) {
if (this.value != 'de' && $("input[value='print']").prop("checked", true) ) {
$("input[value='digital']").prop("checked", true);
$("form .alert").css({display: 'block'});
var target = $('#top');
$('html, body').animate({
scrollTop:$(target).offset().top
},'slow');
e.preventDefault();
}
}
});
});
這是帶有描述的選項卡和選擇列表的頁面:
https://www.academyofsports.de/de/anmeldung/fernstudium.html?product=fitness-c-lizenz
更新了“解決方案”:
// if a country other than germany is elected, set selected tab to digital and scroll to top
$("select[name='country']").on("change", function(e) {
var isDE = this.value == 'de';
if (!$("input[value='print']").is(":checked"))
return;
$("form .alert").toggle(!isDE);
if (isDE)
return;
$("input[value='digital']").prop("checked", true);
$("input[value='print']").prop( "disabled", true );
$("select[name='country']").on('change', function() {
if(isDE){
$("input[value='print']").prop( "disabled", true );
}
else{
$("input[value='print']").prop( "disabled", false );
}
});
var target = $('#top');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 'slow');
});
現在,這種方式可以工作,但是如果重復該過程,則禁用功能將不再起作用。 為什么?
該功能現已啟用,可以在此處進行測試:
https://www.academyofsports.de/de/anmeldung/fernstudium.html?product=fitness-c-lizenz
也許你是這個意思?
在任何情況下都更易於閱讀和修改
$("select[name='country']").on("change", function(e) {
var isDE = this.value == 'de';
$("form .alert").toggle(!isDE);
if (isDE) return;
if (!$("input[value='print']").is(":checked")) return;
e.preventDefault(); // Why?
$("input[value='digital']").prop("checked", true);
var target = $('#top');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 'slow');
});
替代是
$('#top')[0].scrollIntoView();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.