如何从开关按钮获取值(是/否),并在按钮中显示检索到的数据。

4

大家好,

以下是我的代码,用于显示切换按钮“是/否”。我想要的是根据用户的选择来检索数据。如果他们选择了“是”切换按钮,则javascript变量应该有“是”字符串,否则为“否”字符串。

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")); 

我不知道如何获取选定按钮的数据,类似地,在获取数据后,它将存储在数据库中,我想从数据库检索是选中为“是”还是“否”,作为默认值从数据库检索。我该怎么做?我的 jQuery 尝试返回未定义值。请有人帮助我解决这个问题。提前感谢。


如果您需要更多帮助,请提供您已经尝试过的更多代码。 - xAqweRx
我尝试在jQuery中使用类似上面的代码...而不是使用data-on,我尝试使用span id和data-id以及data-off等。 - Kavya Shree
请从开始到结束编写您的任务。 - xAqweRx
3个回答

6

试试这个:

(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

这段内容是关于IT技术的。

是的,它正在工作。我想知道如何在按钮中显示从数据库保存的数据,例如,如果我将其保存为“是”,并且我希望将按钮的默认选择设置为“是”。 - Kavya Shree
试试这个:`// 参数 ($selector, boolean) function setSwitchState(el, flag) { el.attr('checked', flag); }// 用法 setSwitchState($('.switch-input'), true);`
参考:http://jsbin.com/zizuxo/edit?js,console,output
- Surender Lohia
我想检查数据库结果并将其设置为是/否,我该怎么做? - Kavya Shree

2

使用纯JS:

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

jQuery 兼容模式:

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

请告诉我,您在控制台中看到了什么: jQuery.fn.jquery - xAqweRx

0

我将上述内容合并了起来

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

当您已经在使用jQuery时,将其与纯JavaScript结合使用是没有意义的。另外下次,请花时间在您的答案中格式化代码。 - Magiczne
@Magiczne 当你想要在每次按钮切换时检索数据时,它是必需的。唯一的 JavaScript 只会在页面加载时提供数据。感谢您为我编辑代码。 - pankaj kumar
你没有理解我的观点。为什么在jQuery函数内部使用vanilla JS?已接受的答案已经展示了更好的解决方案,只使用jQuery即可。如果您发布了仅使用vanilla JS的解决方案,它将为问题增加一些价值。此外,当页面上有多个.switch-input时,您的解决方案无效。因为它总是从找到的第一个项目中获取值。 - Magiczne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接