【问题标题】:Refresh a Static Div With Javascript使用 Javascript 刷新静态 div
【发布时间】:2017-12-13 10:44:28
【问题描述】:

我很好奇这是否可能......

从我的搜索中,我在 SO 上找到了this 问题,但它似乎与我的困境有点不同。

对于那些不关心查看帖子的人,有人试图通过按下单选按钮(放置在被替换的同一 div 中)来刷新 div。从答案看来,它是成功的。

我的问题

我正在从事以这种方式设置的项目:

  • 单选按钮行,表示他们想要选择一周中的多少天(3 天或 5 天),或仅选择 1 天。
  • 如果用户选择1 day,在不出现的行下会有一个不可见的div。
  • 如果用户选择 3 天或 5 天,则在该行下方显示一个不可见的 div,另一行包含一周中的日期。
  • 如果用户选择3 days,他们只能点击其中的 3 天,而其他 4 天则被禁用(这些“天”按钮是复选框)
  • 如果用户选择5 days,你猜对了,他们可以选择5天。
  • 如果用户随后重新选择3 days,那么他们必须取消选择所有日期,然后重新选择它们才能强制执行 3 天限制,因为他们已经选择了5 days 被选中。
  • 选中复选框时,会在其上激活 CSS 样式,然后其颜色会发生变化。

我要做什么

我正在尝试使我的布局以这种方式工作:

  • 如果用户选择5 days,照常营业。
  • 如果用户然后选择3 days,我希望不可见的div(现在可见,因为one day不是选择的选项)刷新自己:我的意思是有CSS样式已移除且所有复选框均未选中。

我的代码

$("document").ready(function() {
  $('input[name=timing]').on('change', function() {
    if ($(this).prop('id') == "one") {
      $('#recurrence_time').html('One Time');
      $('.day-selections').hide();
    } else if ($(this).prop('id') == "thrice") {
      $('#recurrence_time').html('3 Times a Week');
      $('.day-selections').show();
    } else if ($(this).prop('id') == "once") {
      $('#recurrence_time').html('Once a Week');
      $('.day-selections').hide();
    } else if ($(this).prop('id') == "custom") {
      $('#recurrence_time').html('Custom');
      $('.day-selections').show();
    }
  });
});

$(":checkbox[name=day]").change(function() {
  if ($('input#thrice').is(':checked')) {
    var len = 3;
  } else if ($('input#custom').is(':checked')) {
    var len = 5;
  }
  if ($(":checkbox[name=day]:checked").length == len) {
    $(':checkbox:not(:checked)').prop('disabled', true);
  } else {
    $(':checkbox:not(:checked)').prop('disabled', false);
  }
});

function updateHighlightRadio() {
  var selected = this.parentNode.parentNode.getElementsByClassName("selected")[0];
  if (selected) selected.className = selected.className.replace(" selected", "");
  this.parentNode.className += " selected";
}

window.onload = function() {
  var radios = document.querySelectorAll("input[type=radio]");
  for (var i = 0; i < radios.length; ++i) {
    radios[i].onchange = updateHighlightRadio;
  }
}

$("input[type='checkbox']").change(function() {
  $(this).closest("label").toggleClass(" selected");
});
.selected {
  background-color: #FF8C00;
  color: #FFFFFF;
}

.how-often p {
  font-weight: bold;
  text-align: left;
  display: inline;
  float: left;
}


/* button label */

.recurrence {
  border-radius: 8px;
  padding: 1em;
  margin: 0.5em;
  border-radius: 5px;
  background-color: #ffd199;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  width: 115px;
  text-align: center;
  font-size: 12px;
}


/* remove radio button */

.recurrence input {
  visibility: hidden;
  position: absolute;
}


/* color change when hovering */

.recurrence:hover {
  background-color: #FF8C00;
}

.days {
  border-radius: 8px;
  padding: 1em;
  margin: 0.5em;
  border-radius: 5px;
  background-color: #ffd199;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  width: 75px;
}


/* remove radio button */

.days input {
  visibility: hidden;
  position: absolute;
}


/* color change when hovering */

.days:hover {
  background-color: #FF8C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="how-often pricing">
  <p>How often?</p>
  <label class="recurrence">
                            <input required id="one" name="timing" type="radio">Once
                        </label>
  <label class="recurrence">
                            <input id="once" name="timing" type="radio">Once a week
                        </label>
  <label class="recurrence">
                            <input id="thrice" name="timing" type="radio">3 Times a week
                        </label>
  <label class="recurrence">
                            <input id="custom" name="timing" type="radio">Custom
                        </label>
  <hr>
</div>

<!--start day selection section-->
<div class="day-selections" style="display: none;">
  <p>Which days?</p>
  <label class="days">
                            <input id="mon" name="day" type="checkbox">
                            Monday
                        </label>
  <label class="days">
                            <input id="tue" name="day" type="checkbox">
                            Tuesday
                        </label>
  <label class="days">
                            <input id="wed" name="day" type="checkbox">
                            Wednesday
                        </label>
  <label class="days">
                            <input id="thu" name="day" type="checkbox">
                            Thursday
                        </label>
  <label class="days">
                            <input id="fri" name="day" type="checkbox">
                            Friday
                        </label>
  <hr>
</div>

注意:由于某种原因,在我的 sn-p 中选择按钮后,我无法让按钮保持新颜色...

【问题讨论】:

  • 你能发布一个工作的sn-p吗?
  • 我在向 sn-p 添加更多代码时由于某种原因不断出错...我将在凌晨发布一个 JSFiddle 以便 hide 和show 我的代码函数与 CSS 样式一起工作...... @SergeK.
  • 你为什么要让它变得更复杂,而不是只使用 3 天和 5 天的单独单选按钮?这样如果他们选择了 5 天,然后再次选择 3 天,选中的 5 天复选框将被移除到最初未选中的复选框。
  • 你也可以使用 SO sn-p,就像@GerardoBLANCO 通过edit你的帖子所做的那样
  • 当我尝试向其中添加更多代码时,我无法让它工作,我收到了一个错误。 @Highdef,它们都是单独的单选按钮..我没有按照您所说的复选框将被取消选中。需要详细说明吗? :)

标签: javascript jquery html css


【解决方案1】:

给你。您需要进行的更改:

  • 按照演示添加类和 ID,而不是使用 javascript,您可以使用 CSS :checked 选择器并在选择时更改样式。

  • 保持标签和输入分开而不是嵌套,这样你可以使用标签中的for属性连接到复选框并使用CSS display:none隐藏复选框。

  • 要在选中另一个单选按钮时取消选中复选框,只需确保从所有复选框中删除选中的属性,如每个 if-else 条件 $('input.day').removeAttr('checked'); 的代码所示,比较每个单选按钮的限制值如果超出限制,请使用代码并将其返回为 false,因此无法选择更多复选框。

    if ($(this).siblings(':checked').length >= limit) { this.checked = false; }

$("document").ready(function() {

  $(".day-selections").hide();
  var limit = 0;
  $(':radio').click(function() {
    if ($('.notone').is(':checked')) {
      $(".day-selections").show();
      if ($('#thrice').is(':checked')) {
        limit = 3;
        $('input.day').removeAttr('checked');
        $('input.day').on('change', function(evt) {
          if ($(this).siblings(':checked').length >= limit) {
            this.checked = false;
          }
        });
      } else if ($('#custom').is(':checked')) {
        limit = 5;
        $('input.day').removeAttr('checked');
        $('input.day').on('change', function(evt) {
          if ($(this).siblings(':checked').length >= limit) {
            this.checked = false;
          }
        });
      }
    } else {
      $('input.day').removeAttr('checked');
      $(".day-selections").hide();
    }
  });

});
.selected {
  background-color: #FF8C00;
  color: #FFFFFF;
}

.how-often p {
  font-weight: bold;
  text-align: left;
  display: inline;
  float: left;
}


/* button label */

.recurrence {
  border-radius: 8px;
  padding: 1em;
  margin: 0.5em;
  border-radius: 5px;
  background-color: #ffd199;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  width: 115px;
  text-align: center;
  font-size: 12px;
}


/* remove radio button */

.recurrence input {
  visibility: hidden;
  position: absolute;
}


/* color change when hovering */

.recurrence:hover {
  background-color: #FF8C00;
}

.days {
  border-radius: 8px;
  padding: 1em;
  margin: 0.5em;
  border-radius: 5px;
  background-color: #ffd199;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  width: 75px;
}

.day:checked+.days {
  background-color: #FF8C00;
}

.option {
  display: none;
}

.option:checked+.recurrence {
  background-color: #FF8C00;
}


/* remove radio button */

.day {
  display: none;
}

.days-selection {
  display: flex;
}


/* color change when hovering */

.days:hover {
  background-color: #FF8C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="how-often pricing">
  <p>How often?</p>

  <input required id="one" class="option" name="timing" type="radio">
  <label class="recurrence" for="one">Once</label>

  <input id="once" name="timing" class="option" type="radio">
  <label class="recurrence" for="once">Once a week</label>

  <input id="thrice" class="notone option" name="timing" type="radio">
  <label class="recurrence" for="thrice">3 Times a week</label>

  <input id="custom" class="notone option" name="timing" type="radio">
  <label class="recurrence" for="custom">Custom</label>
  <hr>
</div>
<!--start day selection section-->
<div class="day-selections" style="display: none;">
  <input id="mon" class="day" name="day" type="checkbox">
  <label class="days" for="mon">Monday
	</label>
  <input id="tue" class="day" name="day" type="checkbox">
  <label class="days" for="tue">Tuesday
	</label>
  <input id="wed" class="day" name="day" type="checkbox">
  <label class="days" for="wed">Wednesday
	</label>
  <input id="thu" class="day" name="day" type="checkbox">
  <label class="days" for="thu">Thursday</label>
  <input id="fri" class="day" name="day" type="checkbox">
  <label class="days" for="fri">Friday
	</label>
  <hr>
</div>

【讨论】:

  • 谢谢你,朋友!这个新编辑的限制不起作用,但是我喜欢在取消选中样式后删除样式...仍在努力将其实现到我当前的代码中!完成后我会回复你的! :)
  • 哦等等,一个小故障。
  • 枪之子......这是完美的。十分感谢你的帮助!非常感谢您。
  • @M.Gilbert 修复了问题,if 条件有问题。
  • 再次感谢。我很高兴能将它纳入我的代码并推出!
猜你喜欢
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 2020-02-01
相关资源
最近更新 更多