jquery单选按钮冲突

标签 jquery radio-button session-storage

我有一些脚本,我想在单选按钮更改时更新 div。此外,我将所选单选按钮的值存储在 sessionStorage 中,以便再次访问页面时相应的单选按钮保持选中状态。

我的问题是,如果我使用 sessionStorage 脚本和更新脚本,更新脚本将不再工作,只会保留存储在 session 中的值。

我特别指的是这一行:

inhalt_time = $("input[type='radio'][name='chooseTime']:checked").val();

如果我删除该行:

$("input[type='radio'][name='chooseTime']").val(chosen_time).prop('checked', true);

更新脚本再次起作用。

有谁知道如何避免这种冲突?

这是我的 html:

<div class="chooseTime">
   Bitte w&auml;hlen Sie ihr gew&uuml;nschte Lieferzeit.
   <label class="radio">
      <input type="radio" name="chooseTime" value="06:00 Uhr - 10:00 Uhr" />
      06:00 Uhr - 10:00 Uhr
   </label>
   <label class="radio">
      <input type="radio" name="chooseTime" value="10:00 Uhr - 14:00 Uhr" />
      10:00 Uhr - 14:00 Uhr
   </label>
   <label class="radio">
      <input type="radio" name="chooseTime" value="15:00 Uhr - 17:00 Uhr" />
      15:00 Uhr - 17:00 Uhr
   </label>
</div>

我的更新jquery:

$('.chooseTime').on('change', 'input[type=radio]', function() {
     updateAnzeige();
});

function updateAnzeige() {
    inhalt_time = $("input[type='radio'][name='chooseTime']:checked").val();
    if(inhalt_time === undefined) {
        inhalt_time = "<span class='red'>Bitte wählen Sie eine gewünschte Lieferuhrzeit aus.</span>";
    }
    $('.anzeige').html(inhalt_time);            
}

我的 sessionStorage 的 jquery:

if(sessionStorage.getItem("lieferzeitpunktart") == "Custom") {
    chosen_time = sessionStorage.getItem("lieferzeitpunkt_time");
    $("input[type='radio'][name='chooseTime']").val(chosen_time).prop('checked', true);
}

最佳答案

呵呵!!答案就是这么简单。

通过在 $("input[type='radio'][name='chooseTime']").val(chosen_time).prop(' 中使用 .val(chosen_time) check', true); 我为每个单选按钮指定了相同的值,而不是使用我正在寻找的值来检查单选按钮..

正确的代码行当然是:

$("input[type='radio'][name=chooseTime][value='"+chosen_time+"']").attr('checked', true);

关于jquery单选按钮冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19302954/

相关文章:

javascript - jQuery的scrollTop动画返回Uncaught TypeError

javascript - 在 javascript 中调用我的 wcf 服务时出现 404 - 我缺少什么?

Android RadioButton 类行为

javascript - 页面刷新后如何保持onclick功能

javascript - 使用 localStorage 和 sessionStorage 作为元素树

reactjs - React/Redux 在哪里设置 sessionStorage

jquery - 如何使用jquery提示浏览器显示打开/保存对话框?

javascript - 在 javascript 中触发 css hover

swift - 如何获取 NSStackView 作为 NSAlert 的附属 View ?

JAVA - 单选按钮已选中