如何从切换按钮获取值yes / No并显示按钮激活中的检索数据

时间:2016-05-18 08:01:18

标签: javascript android jquery html css

Hiii Everyone,

下面是我的代码显示切换按钮是/否。我想要做的是基于用户选择我想要检索数据。如果他们选择是切换按钮,javascript变量应该是yes字符串或者没有字符串。

HTML代码

<label class="switch">
  <input class="switch-input" type="checkbox" />
  <span class="switch-label" data-on="Yes" data-off="No"></span> 
  <span class="switch-handle"></span> 
 </label>

CSS

  .switch {
  position: relative;
  display: block;
  vertical-align: top;
  width: 100px;
  height: 30px;
  padding: 3px;
  margin: 0 10px 10px 0;
  background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
  background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
  border-radius: 18px;
  box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
  cursor: pointer;
}
.switch-input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}
.switch-label {
  position: relative;
  display: block;
  height: inherit;
  font-size: 10px;
  text-transform: uppercase;
  background: #eceeef;
  border-radius: inherit;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
}
.switch-label:before, .switch-label:after {
  position: absolute;
  top: 50%;
  margin-top: -.5em;
  line-height: 1;
  -webkit-transition: inherit;
  -moz-transition: inherit;
  -o-transition: inherit;
  transition: inherit;
}
.switch-label:before {
  content: attr(data-off);
  right: 11px;
  color: #aaaaaa;
  text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.switch-label:after {
  content: attr(data-on);
  left: 11px;
  color: #FFFFFF;
  text-shadow: 0 1px rgba(0, 0, 0, 0.2);
  opacity: 0;
}
.switch-input:checked ~ .switch-label {
  background: #0088cc;
  border-color: #0088cc;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.switch-input:checked ~ .switch-label:before {
  opacity: 0;
}
.switch-input:checked ~ .switch-label:after {
  opacity: 1;
}
.switch-handle {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 28px;
  height: 28px;
  background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
  background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
  border-radius: 100%;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.switch-handle:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -6px 0 0 -6px;
  width: 12px;
  height: 12px;
  background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
  background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
  border-radius: 6px;
  box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
}
.switch-input:checked ~ .switch-handle {
  left: 74px;
  box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}

/* Transition
========================== */
.switch-label, .switch-handle {
  transition: All 0.3s ease;
  -webkit-transition: All 0.3s ease;
  -moz-transition: All 0.3s ease;
  -o-transition: All 0.3s ease;
}

我从http://www.htmllion.com/css3-toggle-switch-button.html

重新提供此代码

以下是输出

enter image description here

enter image description here

我尝试了这个jquery来获取:

 alert($(this).attr("data-on")); 

我不知道如何获取选定的按钮数据,并且类似地获取数据后它将存储在数据库中,我想从数据库中检索是或否应该选择它作为默认值从数据库中检索。如何处理this.Result我在jquery中尝试返回未定义的值。请任何人帮助我摆脱这个问题。谢谢提前。

3 个答案:

答案 0 :(得分:4)

试试这个:

(function() {
  $(document).ready(function() {
    $('.switch-input').on('change', function() {
      var isChecked = $(this).is(':checked');
      var selectedData;
      var $switchLabel = $('.switch-label');
      console.log('isChecked: ' + isChecked); 

      if(isChecked) {
        selectedData = $switchLabel.attr('data-on');
      } else {
        selectedData = $switchLabel.attr('data-off');
      }

      console.log('Selected data: ' + selectedData);

    });
  });

})();
  

参考:http://jsbin.com/zizuxo/edit?js,console,output

答案 1 :(得分:0)

在纯JS中:

var result = document.getElementsByClassName("switch-input")[0].checked ? 'yes' : 'no'

兼容模式下的jQuery:

var result = jQuery('.switch-input').is(':checked')?'yes':'no';

答案 2 :(得分:-1)

我结合了以上

$('.switch-input').on('change', function() {
    result = document.getElementsByClassName("switch-input")[0].checked ? 'yes' : 'no';
    alert(result);
});