定义ON和OFF切换按钮的值

时间:2018-06-06 16:15:11

标签: html select input switch-statement toggle

我使用切换开关来替换我的select和2选项代码...

旧代码选择了名称和两个选项,其值为" 0"和" 1"。

如何在此新代码中定义on \ off按钮的值?

例如,我希望" OFF"的值为0和" ON"是1。

我没有使用ajax,只是常规表单。

这是切换开关代码:



html,
body {
  display: flex;
  min-height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: sans-serif;
}

.tgl {
  display: none;
}
.tgl, .tgl:after, .tgl:before, .tgl *, .tgl *:after, .tgl *:before, .tgl + .tgl-btn {
  box-sizing: border-box;
}
.tgl::-moz-selection, .tgl:after::-moz-selection, .tgl:before::-moz-selection, .tgl *::-moz-selection, .tgl *:after::-moz-selection, .tgl *:before::-moz-selection, .tgl + .tgl-btn::-moz-selection {
  background: none;
}
.tgl::selection, .tgl:after::selection, .tgl:before::selection, .tgl *::selection, .tgl *:after::selection, .tgl *:before::selection, .tgl + .tgl-btn::selection {
  background: none;
}
.tgl + .tgl-btn {
  outline: 0;
  display: block;
  width: 4em;
  height: 2em;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}
.tgl + .tgl-btn:after, .tgl + .tgl-btn:before {
  position: relative;
  display: block;
  content: "";
  width: 50%;
  height: 100%;
}
.tgl + .tgl-btn:after {
  right: -50%;
}
.tgl + .tgl-btn:before {
  display: none;
}
.tgl:checked + .tgl-btn:after {
  right: 50%;
}

.tgl-flat + .tgl-btn {
  padding: 2px;
  transition: all .2s ease;
  background: #fff;
  border: 4px solid #f2f2f2;
  border-radius: 2em;
}
.tgl-flat + .tgl-btn:after {
  transition: all .2s ease;
  background: #f2f2f2;
  content: "";
  border-radius: 1em;
}
.tgl-flat:checked + .tgl-btn {
  border: 4px solid #7FC6A6;
}
.tgl-flat:checked + .tgl-btn:after {
  right: 0%;
  background: #7FC6A6;
}

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

<head>
  <meta charset="UTF-8">
  <title>Pure CSS toggle buttons</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"> 

</head>
<body>
    <h4>Toggle Switch</h4>
    <input class="tgl tgl-flat" id="cb4" class="radio" value="1" name="fooby[1][]" type="checkbox" checked />
    <label class="tgl-btn" for="cb4"></label>
</body>

</html>
&#13;
&#13;
&#13;

旧代码是:

<select class="select" name="notifications" type="text">
   <option value="1" <? if ($notifications == 1) echo "selected"; ?>>ON</option>
   <option value="0" <? if ($notifications == 0) echo "selected"; ?>>OFF</option>
</select>

2 个答案:

答案 0 :(得分:0)

那么你问的是如何处理拨动开关?我在你的代码中添加了这个javascript:

$("#cb4").change(function() {
    if ($(this).is(":checked")) {
      //ON
      console.log("on");
    } else {
      //OFF
      console.log("off");
    }
});

代码段:

$("#cb4").change(function() {
  if ($(this).is(":checked")) {
      //ON
      console.log("on");
    } else {
      //OFF
      console.log("off");
    }
});
html,
body {
  display: flex;
  min-height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: sans-serif;
}

.tgl {
  display: none;
}
.tgl, .tgl:after, .tgl:before, .tgl *, .tgl *:after, .tgl *:before, .tgl + .tgl-btn {
  box-sizing: border-box;
}
.tgl::-moz-selection, .tgl:after::-moz-selection, .tgl:before::-moz-selection, .tgl *::-moz-selection, .tgl *:after::-moz-selection, .tgl *:before::-moz-selection, .tgl + .tgl-btn::-moz-selection {
  background: none;
}
.tgl::selection, .tgl:after::selection, .tgl:before::selection, .tgl *::selection, .tgl *:after::selection, .tgl *:before::selection, .tgl + .tgl-btn::selection {
  background: none;
}
.tgl + .tgl-btn {
  outline: 0;
  display: block;
  width: 4em;
  height: 2em;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}
.tgl + .tgl-btn:after, .tgl + .tgl-btn:before {
  position: relative;
  display: block;
  content: "";
  width: 50%;
  height: 100%;
}
.tgl + .tgl-btn:after {
  right: -50%;
}
.tgl + .tgl-btn:before {
  display: none;
}
.tgl:checked + .tgl-btn:after {
  right: 50%;
}

.tgl-flat + .tgl-btn {
  padding: 2px;
  transition: all .2s ease;
  background: #fff;
  border: 4px solid #f2f2f2;
  border-radius: 2em;
}
.tgl-flat + .tgl-btn:after {
  transition: all .2s ease;
  background: #f2f2f2;
  content: "";
  border-radius: 1em;
}
.tgl-flat:checked + .tgl-btn {
  border: 4px solid #7FC6A6;
}
.tgl-flat:checked + .tgl-btn:after {
  right: 0%;
  background: #7FC6A6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Pure CSS toggle buttons</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"> 

</head>
<body>
    <h4>Toggle Switch</h4>
    <input class="tgl tgl-flat" id="cb4" class="radio" value="1" name="fooby[1][]" type="checkbox" checked />
    <label class="tgl-btn" for="cb4"></label>
</body>

</html>

答案 1 :(得分:0)

我认为以下问题的答案就是你要找的。

How to make a radio button look like a toggle button