- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了两个选择框 - 一个控制另一个的可见性。在页面加载时,受控选择框(#select02)
的功能与我想要的完全一样,只要我通过选择控制选择框(select01)中的选项,不显示/隐藏相关选择框
.(#select02)
一旦我这样做 - 受控选择框(#select02)
中的所有功能都将停止运行。
所以我的问题是 - 如何维护“受控”选择框的功能(#select02)
,在显示/隐藏它之后?
对于那些喜欢强制性实时代码的人......HTML:
<div>
<h1>The controlling box</h1>
<p>(play with this second)</p>
<select id = "select01" size = 2>
<option value="value01">Hide Other Div</option>
<option value="value02">Show Other Div</option>
</select>
</div><!--end section-->
<div id = "slide">
<h1>The controlled box</h1>
<p>(play with this first)</p>
<select size = "4" id = "select02" class = "moduleSelect" multiple>
<option class = "module 30" value = "2">OptionA</option>
<option class = "module 30" value = "2">OptionB</option>
<option class = "module 15" value = "1">OptionC</option>
<option class = "module 15" value = "1">OptionD</option>
</select>
<div id = "hidden">
<b><i class = "hiddenHeader">You have chosen:</i><p id = "userChoice"></p></b>
<b><i class = "hiddenHeader">This Total:</i><p id = "Tot"></p></b>
<p id = "Status"></p>
<a href="javascript:void(0)" id="remove" class = "remove"><p>RESELECT</p></a>
</div><!--END #HIDDEN01-->
</div><!--END #SLIDE-->
脚本:
$(document).ready(function(){
//TURN OFF ALL SELECTIONS IN THE OPTIONS (NO ISSUE IT SEEMS)
$("option:selected").removeAttr("selected");
$(".moduleSelect").prop("disabled", false);
$(".module").prop("disabled", false);
//DECLARE THE SELECT TOTAL VARIABLE
var Total = 0;
//THE HIDE FUNCTIONALITY EXECUTED BY THE "CONTROLLING" SELECT BOX
//(OSTENSIBLY THE ISSUE, IT SEEMS)
if($('#select01').val() == 'value01'){
$('#slide').slideUp('slow', 'swing');
}
$('#select01').change(function(){
if($(this).val() == 'value02'){
$('#slide').slideDown('slow', 'swing');
return true;
}
$('#slide').slideUp('slow', 'swing');
});
//THE USER FEEDBACK FUNTIONALITY PROVIDED BY THE "CONTROLLED" SELECT BOX
//(NO ISSUE, IT SEEMS)
//WHEN A USER CLICKS ON ANY OPTION
$("#select02").change(function(){
//TOGGLE MULTIPLE SELECTION
$("#select02 option").click(function(){
$(this).toggleClass("selected");
});
if (Total <30){
if ($("option:selected").val() == "0"){
Total += 0;
}
else if ($("option:selected").val() == "1"){
Total += 15;
}
else if ($("option:selected").val() == "2"){
Total += 30;
}
else if ($("option:selected").val() == ""){
Total = 0;
}
if (Total == 30){
$(this).prop("disabled", true);
$("#Tot").text(Total);
$("#Status").html("<i>(You have selected the maximum required number of Credits)</i>");
$("#hidden").css('display', 'block');
$("#userChoice").append("<li>" + $("option:selected", this).text() + "</li>");
}
if (Total == 15){
$(this).children(".30").prop("disabled", true);
$("#Tot").text(Total);
$("#Status").text("Select another 15 credits");
$("#hidden").css('display', 'block');
$("#userChoice").append("<li>" + $("option:selected", this).text() + "</li>");
}
}else{
$("#Status").text("You have already selected the required number of Credits");
}
});
// THE RESELECT FUNCTION TO CLEAR
//(NO ISSUE, IT SEEMS)
$("div a#remove").click(function () {
$("#select02 option:selected").removeAttr("selected");
$("#select02").prop("disabled", false);
$("#select02").children(".30").prop("disabled", false);
$("#hidden").css('display', 'none');
$("#userChoice").empty();
$("#Tot").empty();
$("#Status").empty();
Total = 0;
});
});
请记住,我是自学成才的 Web 开发人员,完全是菜鸟,而且可能做了一些根本错误的事情! :)还请原谅我糟糕的编程......
提前感谢你们中那些热心的问题解决者。科利G
最佳答案
我有updated the fiddle 。基本上,以下代码有问题:
$("select").change(function(){
$("option:selected").val();
});
此代码获取文档中第一个选定选项的值。这解释了为什么当您从第一个选择中选择某些内容时,第二个选择的代码不起作用。
<小时/>话虽如此,更好的解决方案是在用户选择或取消选择选项时重新计算总数。使用jQuery.val()
它返回选定选项的数组。所以我们有:
$("#select02").change(function() {
var Total = 0;
$.each($(this).val(), function(_, value) {
if (value === "1") {
Total += 15;
} else if (value === "2") {
Total += 30;
}
});
});
获得总数后,您可以更新显示。
关于javascript - jQuery 显示/隐藏影响同级选择框功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123320/
我正在尝试使此 OSX 代码(golfed 以便于讨论)在 Ubuntu Linux 上运行。 cat >main.c #include #include void provided_by_ma
带有 .next() jquery 方法的小选择器让我发疯。 故事是这样的,我在表行上使用 Bootstrap 过滤器和 jquery。当过滤器执行时,它会将显示样式属性更改为 table-row,如
无法理解为什么此代码将 DIV 元素的颜色更改为蓝色,但不更改 SPAN 元素的颜色。有什么想法吗? $(document).ready(function(){ $
这个问题在这里已经有了答案: How can I target a specific group of siblings in a flat hierarchy? (1 个回答) 关闭 6 年前。
请看here ,左侧的菜单应该在复选框选中的属性上滑入和滑出,但我的 CSS 不工作,任何人都可以帮助我。在我的代码菜单中,当前设置为 left:-240px,因为它应该滑入 left:0 选中复选框
我正在创建的这个系统遇到了问题。我总共有四个兄弟 div 容器。它们在普通桌面 View 中是这样的: 蓝红蓝红 这适用于 2/3 视口(viewport)以将 background-color 显示
使用 UITabBar,我有 4 个同级 View (每个选项卡项目一个)。加载应用程序时,第一个选项卡项目和 View 可见。第一个 View 有一个发布 NSNotification 的 IBAc
这个问题已经有答案了: Self-references in object literals / initializers (31 个回答) 已关闭 9 年前。 我有一个json结构如下:
我有多个带有标题、描述和号召性用语的产品。每个的字数没有规定。我正在使用 flexbox 来展示这些产品,这样每个元素的容器高度相等,并且可以轻松地清除到下一行,而不必弄乱第 nth-child。 我
我正在努力解决 IE7 问题。我希望我的兄弟 div 与其 sibling 的宽度相同。第一个 sibling 充当标题,而其他 sibling 有一定的尺寸。我可以使用一些建议。 http://js
我正在创建 3 行,每行有 4 列。但我想要实现的是,当 1 调整一行中任何列的大小时,其余列应根据行的总剩余宽度调整大小。假设如果行的总宽度为 100%,并且如果我将第一列的大小调整 60%,则剩余
到目前为止,我有以下cinoptions: cino= cino+=:0 cino+=g0 cino+=p0 cino+=(0 cino+={0 cino+=l1 cino+=t0 cino+=u2
如果我没有解释清楚,请提前道歉。我会尽力澄清我正在尝试做的事情。事不宜迟...假设您在页面上有一系列数字(仅由一个空格分隔),其中括号表示您所在的当前页面。 在页面上看起来是这样的: [1] 2 3
我的 parent 中有两个 sibling ,并且 sibling 的数据正在更新,数据已更新,但是 ngOnInit(){} 未调用。 https://stackblitz.com/edit/an
我正在创建一个可滑动的菜单,它可以正常工作,但有一个方面除外。我希望菜单始终具有 100% 的高度……无论用户在页面上向下滚动多远。但是,每当我将其设置为 height: 100%; 时,菜单都不会滑
first-child last-child first-child-1 last-chi
我想显示一个评论列表以及每个评论的评论列表等等: 这是我的 HTML 代码: Titre d'un commentaire statique Qui cum ven
我进行了彻底的搜索,但没有找到任何关于如何查询和检索多个 XML 同级的值的示例或指导。 有很多关于如何检索单个兄弟值的示例。 例如,给出以下 XML 片段: 800000 80000
这个问题已经有答案了: Is there a "previous sibling" selector? (33 个回答) 已关闭 7 年前。 在 CSS 中,如果同级元素位于输出中的上方(HTML 中
console.log((this).sibling('.advanceOptWrap')); 以上代码返回 Uncaught TypeError: $(...).sibling is not a f
我是一名优秀的程序员,十分优秀!