删除单选按钮中的默认选中值并将其替换为单击的另一个单选按钮 [英] Remove the default checked value in radio button and replace it with the another radio button that is click

查看:53
本文介绍了删除单选按钮中的默认选中值并将其替换为单击的另一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将单选按钮中的默认检查值更改为用户选择的值.因为每当我尝试它时,它仍然会在控制台日志中显示默认检查值而不是检查者.我想将默认检查更改为用户在 HTML 中选择的内容.谁能帮我?我只是一个新手.任何帮助,将不胜感激.谢谢.这是代码.CSS:

 html,身体 {字体:400 小写字母 16px/1.25 Arial;}字段集{宽度:适合内容;填充:0;}传奇 {字体大小:1rem}细节 {宽度:150px;光标:指针;边距:0 4px -5px 0;填充:0 0 0 10px;}概括 {位置:相对;宽度:96%;轮廓:0.5px 脊灰;}/*隐藏  的默认箭头*/细节摘要::-webkit-details-marker {可见性:隐藏;位置:绝对;z-索引:-1;}/*|伪-<选项>所有伪<选项>最初是隐藏的和<label class='opt'>是唯一会显示/隐藏的标签,下一条评论解释了如何.*/.rad {显示:无}.opt {显示:无;边距:0 0 2px 0;光标:指针;字体大小:0.9rem;box-shadow: -2px -2px 11px rgba(0, 0, 0, 0.3) 插入;}/*|两个条件1.如果<details open='true'>所有 

HTML

<html lang="zh-cn"><头><meta charset="UTF-8"><title>自定义选择框 Jquery Plugin by VJ</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"><!--|标志图标此样式表提供标志图标.详情请前往:https://afeld.github.io/emoji-css/--><link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet"/><身体><form id="容器"><字段集><legend>国家</legend><!--|伪-<选择><详情>提供下拉行为和<总结>包含伪<option>--><详情><总结><!--|4 伪-<选项>每个<标签>和 <input type='radio'>对是通过匹配的值相互同步<label for="ID">和<input id="ID">.--><!--|触发器和状态当<标签>被点击...<input type='radio'>被检查.这个简单的因果"可以被用于状态系统(即关闭/开启).有关详细信息,请查看 CSS.--><input id='X' type='radio' class='rad' name='rad' value=""checked><label class='opt' for='X'>&nbsp;&nbsp;<i class='em em-us'></i>美国<input id='US' type='radio' class='rad' name='rad' value="美国">

JAVASCRIPT

解决方案

localStorage

使用 localStorage 保存选定的值,然后在重新加载页面时检索该值.注意:有关此演示其余部分如何工作的详细信息,请参阅此帖子.><块引用>

var xC = document.forms.container;var xE = xC.elements;var vR = xE.rad;var cty = localStorage.getItem('country');xC.addEventListener('change', saveCountry);document.addEventListener('DOMContentLoaded', setCountry);函数 saveCountry(e) {localStorage.setItem('country', vR.value);console.log("欢迎," + vR.value);};函数 setCountry(e) {for (让 i = 0; i < vR.length; i++) {if (vR[i].value === cty) {vR[i].checked = true;}}console.log("欢迎," + vR.value);};


Plunker

演示

由于 SO 限制 localStorage,此演示无法完全运行. 如需功能齐全的演示,请查看此 Plunker.要进行测试,请选择一个国家/地区,然后通过单击预览面板中的刷新按钮或 CTRL+ENTER(PC 或 )刷新页面+返回 Mac.

详情在demo中评论

var xC = document.forms.container;var xE = xC.elements;var vR = xE.rad;/*获取保存在名为 'country' 的 localStorage 中的字符串*/var cty = localStorage.getItem('country');/*|当<表格>改变了调用 saveCountry()*/xC.addEventListener('change', saveCountry);/*|页面重新加载时调用 setCountry()*/document.addEventListener('DOMContentLoaded', setCountry);/*保存在伪<select>中选择的值在本地存储中*/函数 saveCountry(e) {localStorage.setItem('country', vR.value);console.log("欢迎," + vR.value);};/*循环遍历伪<option>s当伪<选项>匹配保存的值...在 localStorage 中以国家"的名义...检查伪<选项>通过分配它...属性 [checked] = true*/函数 setCountry(e) {for (让 i = 0; i < vR.length; i++) {if (vR[i].value === cty) {vR[i].checked = true;}}console.log("欢迎," + vR.value);};

html,身体 {字体:400 小写字母 16px/1.25 Arial;}字段集{宽度:适合内容;填充:0;}传奇 {字体大小:1rem}细节 {宽度:170px;光标:指针;边距:0 4px -5px 0;填充:0 5px 10px 10px;}概括 {宽度:100%;位置:相对;}/*显示 <detail> 的默认箭头*//*摘要::-webkit-details-marker {位置:绝对;填充:0 0 0 5px;z-索引:1;顶部:25%;}[打开] 摘要::-webkit-details-marker {前5%;}.em {填充:0 0 0 10px;}*//*隐藏  的默认箭头*/摘要::-webkit-details-marker {可见性:隐藏;位置:绝对;z-索引:-1;}.em {填充:0;}/*|伪-<选项>所有伪<选项>最初是隐藏的和<label class='opt'>是唯一会显示/隐藏的标签,下一条评论解释了如何.*/.rad {显示:无}.opt {显示:无;位置:相对;z-索引:2;宽度:100%;边距:0;光标:指针;字体大小:0.9rem;box-shadow: -2px -2px 11px rgba(0, 0, 0, 0.3) 插入;}/*|两个条件1.如果<details open='true'>所有 

<html lang="zh-cn"><头><meta charset="UTF-8"><title>自定义选择框 Jquery Plugin by VJ</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"><!--|标志图标此样式表提供标志图标.详情请前往:https://afeld.github.io/emoji-css/--><link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet"><身体><form id="容器"><字段集><legend>国家</legend><!--|伪-<选择><详情>提供下拉行为和<总结>包含伪<option>--><详情><总结><!--|4 伪-<选项>每个<标签>和 <input type='radio'>对是通过匹配的值相互同步<label for="ID">和<input id="ID">.--><!--|触发器和状态当<标签>被点击...<input type='radio'>被检查.这个简单的因果"可以被用于状态系统(即关闭/开启).有关详细信息,请查看 CSS.--><input id='X' type='radio' class='rad' name='rad' value=""checked><label class='opt' for='X'>&nbsp;&nbsp;<i class='em em-us'></i>美国<input id='US' type='radio' class='rad' name='rad' value="美国">

I just want to change the default check value in radio button to what is the user selected value.Because whenever i try it still display in console log the default checked and not the one who is check. i want to change default checked to what user selected in HTML. Can anyone help me? im just really a newbie. Any help would be appreciated. Thank you.Here is the code. CSS:

    html,
    body {
      font: 400 small-caps 16px/1.25 Arial;
    }

    fieldset {
      width: fit-content;
      padding: 0;
    }

    legend {
      font-size: 1rem
    }

    details {
      width: 150px;
      cursor: pointer;
      margin: 0 4px -5px 0;
      padding: 0 0 0 10px;
    }

    summary {
      position: relative;
      width: 96%;
      outline: 0.5px ridge grey;
    }


    /*
    Hides <detail>'s default arrow
    */

    details summary::-webkit-details-marker {
      visibility: hidden;
      position: absolute;
      z-index: -1;
    }


    /*| Pseudo-<option>
    All pseudo-<option> are initially hidden and 
    <label class='opt'> are the only tags that will show/hide, 
    the next comment explains how.
    */

    .rad {
      display: none
    }

    .opt {
      display: none;
      margin: 0 0 2px 0;
      cursor: pointer;
      font-size: 0.9rem;
      box-shadow: -2px -2px 11px rgba(0, 0, 0, 0.3) inset;
    }


    /*| Two Conditions
    1. If <details open='true'> all <label class='opt'> are visible.
    =============================================
    2. if <input class='rad' type='radio' checked> then the 
       <label class='opt'> that proceeds the radio button is visible.
    */

    [open] .opt,
    .rad:checked+.opt {
      display: block;
    }

    /*| For Demonstration Purposes
    This ruleset changes how the console is displayed.
    */

    .as-console-wrapper {
      width: 50%;
      min-height: 100%;
      margin-left: 50%;
      font-variant: normal;
    }

    .as-console-row.as-console-row::after {
      content: '';
      padding: 0;
      margin: 0;
      border: 0;
      width: 0;
    }
</style>

HTML

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Custom select box Jquery Plugin by VJ</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

<!--| Flag Icons
This stylesheet provides the flag icons. 
For details, go to: 
https://afeld.github.io/emoji-css/
-->

  <link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet" />
</head>

<body>
 <form id="container">
  <fieldset>
    <legend>Country</legend>

<!--| Pseudo-<select> 
<details> provides the dropdown behavior and
<summary> contains the pseudo-<option>
-->
    <details>
     <summary>

<!--| 4 Pseudo-<option> 
Each <label> and <input type='radio'> pair are
synced to each other by matching the values of
<label for="ID"> and <input id="ID">. 
-->

<!--| Trigger and State
When <label> is clicked ... <input type='radio'>
is checked. This simple "cause and effect" can
be leveraged into a system of states (ie off/on). 
For details, review the CSS. 
-->

  <input id='X' type='radio' class='rad' name='rad' value="" checked>
  <label class='opt' for='X'>
    &nbsp;&nbsp;
    <i class='em em-us'></i> 
    United States
  </label>

  <input id='US' type='radio' class='rad' name='rad' value="United States">
  <label class='opt' for='US'>
    &nbsp;&nbsp;
    <i class='em em-us'></i> 
    United States
  </label>

  <input id='GB' type='radio' class='rad' name='rad' value="Great Britain">
  <label class='opt' for='GB'>
    &nbsp;&nbsp;
    <i class='em em-gb'></i> 
    Great Britain
  </label>

  <input id='IN' type='radio' class='rad' name='rad' value="India">
  <label class='opt' for='IN'>
    &nbsp;&nbsp;
    <i class='em em-flag-in'></i>
    India
  </label>

  <input id='NP' type='radio' class='rad' name='rad' value="Nepal">
  <label class='opt' for='NP'>
    &nbsp;&nbsp;
    <i class='em em-flag-np'></i>
    Nepal
  </label>

  </summary>
</details>
</fieldset>
</form>

</body>
</html>

JAVASCRIPT

<script type="text/javascript">
    var xC = document.forms.container;
    var xE = xC.elements;
    var vR = xE.rad;

    xC.onchange = function() {
      // console.log(vR.value);
      // location.reload();
      if(vR.value=="United States") {
        alert("welcome us");
        location.reload();
      }
      else if(vR.value=="United States") {
        alert("welcome us");
        location.reload();
      }

    }
</script>

解决方案

localStorage

Use localStorage to save the selected value then retrieve the value when page is reloaded. Note: For details on how the rest of this demo works, see this post.

var xC = document.forms.container;
var xE = xC.elements;
var vR = xE.rad;
var cty = localStorage.getItem('country');
xC.addEventListener('change', saveCountry);
document.addEventListener('DOMContentLoaded', setCountry);
function saveCountry(e) {
  localStorage.setItem('country', vR.value);
  console.log("Welcome, " + vR.value);
};
function setCountry(e) {
  for (let i = 0; i < vR.length; i++) {
    if (vR[i].value === cty) {
      vR[i].checked = true;
    }
  }
  console.log("Welcome, " + vR.value);
};


Plunker

Demo

This demo does not fully function due to SO restricting localStorage. For a fully functional demo, review this Plunker. To test it, select a country, then refresh the page by clicking the refresh button in the preview panel or CTRL+ENTER for PC or +RETURN for Mac.

Details are commented in demo

var xC = document.forms.container;
var xE = xC.elements;
var vR = xE.rad;
/* 
Get the string saved in localStorage named 'country'
*/
var cty = localStorage.getItem('country');

/*| When <form> is changed
Call saveCountry()
*/
xC.addEventListener('change', saveCountry);

/*| When Page is Reloaded
Call setCountry()
*/
document.addEventListener('DOMContentLoaded', setCountry);

/*
Save the value selected in pseudo-<select> in localStorage
*/
function saveCountry(e) {
  localStorage.setItem('country', vR.value);
  console.log("Welcome, " + vR.value);
};

/*
Loop through the pseudo-<option>s
When a pseudo<option> matches the value saved...
in localStorage under the name of 'country'...
check that pseudo-<option> by assigning it...
the attribute [checked] = true
*/
function setCountry(e) {
  for (let i = 0; i < vR.length; i++) {
    if (vR[i].value === cty) {
      vR[i].checked = true;
    }
  }
  console.log("Welcome, " + vR.value);
};

html,
body {
  font: 400 small-caps 16px/1.25 Arial;
}

fieldset {
  width: fit-content;
  padding: 0;
}

legend {
  font-size: 1rem
}

details {
  width: 170px;
  cursor: pointer;
  margin: 0 4px -5px 0;
  padding: 0 5px 10px 10px;
}

summary {
  width: 100%;
  position: relative;
}


/* 
    Shows <detail>'s default arrow
    */


/*
    summary::-webkit-details-marker {
      position: absolute;
      padding: 0 0 0 5px;
      z-index: 1;
      top: 25%;
    }
    
    [open] summary::-webkit-details-marker {
      top: 5%;
    }
    
    .em {
      padding: 0 0 0 10px;
    }
    */


/*
    Hides <detail>'s default arrow
    */

summary::-webkit-details-marker {
  visibility: hidden;
  position: absolute;
  z-index: -1;
}

.em {
  padding: 0;
}


/*| Pseudo-<option>
    All pseudo-<option> are initially hidden and 
    <label class='opt'> are the only tags that will show/hide, 
    the next comment explains how.
    */

.rad {
  display: none
}

.opt {
  display: none;
  position: relative;
  z-index: 2;
  width: 100%;
  margin: 0;
  cursor: pointer;
  font-size: 0.9rem;
  box-shadow: -2px -2px 11px rgba(0, 0, 0, 0.3) inset;
}


/*| Two Conditions
    1. If <details open='true'> all <label class='opt'> are visible.
    =============================================
    2. if <input class='rad' type='radio' checked> then the 
       <label class='opt'> that proceeds the radio button is visible.
    */

[open] .opt,
.rad:checked+.opt {
  display: block;
}


/*| For Demonstration Purposes
    This ruleset changes how the console is displayed.
    */

.as-console-wrapper {
  max-height: 50%;
  font-variant: normal;
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Custom select box Jquery Plugin by VJ</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <!--| Flag Icons
This stylesheet provides the flag icons. 
For details, go to: 
https://afeld.github.io/emoji-css/
-->

  <link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet">


</head>

<body>
  <form id="container">
    <fieldset>
      <legend>Country</legend>

      <!--| Pseudo-<select> 
<details> provides the dropdown behavior and
<summary> contains the pseudo-<option>
-->
      <details>
        <summary>

          <!--| 4 Pseudo-<option> 
Each <label> and <input type='radio'> pair are
synced to each other by matching the values of
<label for="ID"> and <input id="ID">. 
-->

          <!--| Trigger and State
When <label> is clicked ... <input type='radio'>
is checked. This simple "cause and effect" can
be leveraged into a system of states (ie off/on). 
For details, review the CSS. 
-->

          <input id='X' type='radio' class='rad' name='rad' value="" checked>
          <label class='opt' for='X'>
            &nbsp;&nbsp;
            <i class='em em-us'></i> United States
          </label>

          <input id='US' type='radio' class='rad' name='rad' value="United States">
          <label class='opt' for='US'>
            &nbsp;&nbsp;
            <i class='em em-us'></i> United States
          </label>

          <input id='GB' type='radio' class='rad' name='rad' value="Great Britain">
          <label class='opt' for='GB'>
            &nbsp;&nbsp;
            <i class='em em-gb'></i> Great Britain
          </label>

          <input id='IN' type='radio' class='rad' name='rad' value="India">
          <label class='opt' for='IN'>
            &nbsp;&nbsp;
            <i class='em em-flag-in'></i> India
          </label>

          <input id='NP' type='radio' class='rad' name='rad' value="Nepal">
          <label class='opt' for='NP'>
            &nbsp;&nbsp;<i class='em em-flag-np'></i>&nbsp;&nbsp;Nepal
          </label>

        </summary>
      </details>

    </fieldset>
  </form>

  <!--|For Demonstration Purposes
Provides console
-->


</body>

</html>

这篇关于删除单选按钮中的默认选中值并将其替换为单击的另一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆