Javascript 引导开关如何在单击事件时读取数据并将其打印
我已经实现了BootStrap开关,如下所示:Javascript 引导开关如何在单击事件时读取数据并将其打印,javascript,jquery,ajax,twitter-bootstrap,Javascript,Jquery,Ajax,Twitter Bootstrap,我已经实现了BootStrap开关,如下所示: <?php while ($arr = mysql_fetch_array($res)) { ?> <td class="center"> <?php if ($arr['status'] == 1) { ?> <input class="switch" type="checkbox" data-on-label="YES" data
<?php while ($arr = mysql_fetch_array($res)) { ?>
<td class="center">
<?php if ($arr['status'] == 1) { ?>
<input class="switch" type="checkbox" data-on-label="YES" data-off-label="NO" name="my-checkbox" checked>
<?php } else { ?>
<input class="switch" type="checkbox" data-on-label="SI" data-off-label="NO" name="my-checkbox">
<?php } ?>
</td>
在这种情况下,我可以获得状态,但我也希望获得数据(将是post id),因为我希望发送Ajax调用
或者是否有其他方法可以在开关上单击“使用ajax更新数据库值”如果您已将数据附加到元素,则可以执行此操作
$(document).ready(function () {
$('.switch').on('switchChange.bootstrapSwitch',function (event, state) {
console.log($(event.element).attr("data"));
});
});
如果已将数据附着到图元,则可以执行此操作
$(document).ready(function () {
$('.switch').on('switchChange.bootstrapSwitch',function (event, state) {
console.log($(event.element).attr("data"));
});
});
如果已将数据附着到图元,则可以执行此操作
$(document).ready(function () {
$('.switch').on('switchChange.bootstrapSwitch',function (event, state) {
console.log($(event.element).attr("data"));
});
});
如果已将数据附着到图元,则可以执行此操作
$(document).ready(function () {
$('.switch').on('switchChange.bootstrapSwitch',function (event, state) {
console.log($(event.element).attr("data"));
});
});
下面是获取复选框的状态、文本和数据的解决方案
$(函数(){
变量开关选择器='输入[type=“checkbox”].bs开关';
//将类名为“bs switch”的所有复选框转换为开关。
$(开关选择器).bootstrapSwitch();
//将“switchChange”事件附加到所有交换机。
$(switchSelector).on('switchChange.bootstrapSwitch',函数(事件,状态){
console.log(this);//DOM元素
console.log(event);//jQuery事件
console.log(state);//真|假
//获取交换机的信息。
变量信息={
州:州,,
值:$(此).data(状态为?'onText':'offText'),
数据:$(this.attr('data'))
}
//显示引导信息警报。
如果($('div.alert')。长度===0){
$('body').append($('').addClass('alert-alert-info');
}
$('div.alert').text(JSON.stringify(info,undefined');
});
});代码>
正文{
填充:20px;
}
.alert.alert-info{
边缘顶部:1米;
空白:预处理;
}
以下是获取复选框状态、文本和数据的解决方案
$(函数(){
变量开关选择器='输入[type=“checkbox”].bs开关';
//将类名为“bs switch”的所有复选框转换为开关。
$(开关选择器).bootstrapSwitch();
//将“switchChange”事件附加到所有交换机。
$(switchSelector).on('switchChange.bootstrapSwitch',函数(事件,状态){
console.log(this);//DOM元素
console.log(event);//jQuery事件
console.log(state);//真|假
//获取交换机的信息。
变量信息={
州:州,,
值:$(此).data(状态为?'onText':'offText'),
数据:$(this.attr('data'))
}
//显示引导信息警报。
如果($('div.alert')。长度===0){
$('body').append($('').addClass('alert-alert-info');
}
$('div.alert').text(JSON.stringify(info,undefined');
});
});代码>
正文{
填充:20px;
}
.alert.alert-info{
边缘顶部:1米;
空白:预处理;
}
以下是获取复选框状态、文本和数据的解决方案
$(函数(){
变量开关选择器='输入[type=“checkbox”].bs开关';
//将类名为“bs switch”的所有复选框转换为开关。
$(开关选择器).bootstrapSwitch();
//将“switchChange”事件附加到所有交换机。
$(switchSelector).on('switchChange.bootstrapSwitch',函数(事件,状态){
console.log(this);//DOM元素
console.log(event);//jQuery事件
console.log(state);//真|假
//获取交换机的信息。
变量信息={
州:州,,
值:$(此).data(状态为?'onText':'offText'),
数据:$(this.attr('data'))
}
//显示引导信息警报。
如果($('div.alert')。长度===0){
$('body').append($('').addClass('alert-alert-info');
}
$('div.alert').text(JSON.stringify(info,undefined');
});
});代码>
正文{
填充:20px;
}
.alert.alert-info{
边缘顶部:1米;
空白:预处理;
}
以下是获取复选框状态、文本和数据的解决方案
$(函数(){
变量开关选择器='输入[type=“checkbox”].bs开关';
//将类名为“bs switch”的所有复选框转换为开关。
$(开关选择器).bootstrapSwitch();
//将“switchChange”事件附加到所有交换机。
$(switchSelector).on('switchChange.bootstrapSwitch',函数(事件,状态){
console.log(this);//DOM元素
console.log(event);//jQuery事件
console.log(state);//真|假
//获取交换机的信息。
变量信息={
州:州,,
值:$(此).data(状态为?'onText':'offText'),
数据:$(this.attr('data'))
}
//显示引导信息警报。
如果($('div.alert')。长度===0){
$('body').append($('').addClass('alert-alert-info');
}
$('div.alert').text(JSON.stringify(info,undefined');
});
});代码>
正文{
填充:20px;
}
.alert.alert-info{
边缘顶部:1米;
空白:预处理;
}
它将更新复选框的值。它在引导开关中显示问题。此代码将解决您的问题
<input type="checkbox" tabindex="6" name="autopopup" value="0"
data-bootstrap-switch data-off-color="danger" id="autopopup" data-on-color="success">
$('#multiselect').on('switchChange.bootstrapSwitch',function (event, state) {
var checkBox = document.getElementById("multiselect");
// If the checkbox is checked, display the output text
if (checkBox.checked == true){
document.getElementById("multiselect").value = "1";
} else {
document.getElementById("multiselect").value = "0";
}
});
$('#multiselect').on('switchChange.bootstrapSwitch',函数(事件、状态){
var checkBox=document.getElementById(“multiselect”);
//如果选中该复选框,则显示输出文本
如果(checkBox.checked==true){
document.getElementById(“multiselect”).value=“1”;
}否则{
document.getElementById(“multiselect”).value=“0”;
}
});
它将更新复选框的值。它在引导开关中显示问题。此代码将解决您的问题
<input type="checkbox" tabindex="6" name="autopopup" value="0"
data-bootstrap-switch data-off-color="danger" id="autopopup" data-on-color="success">
$('#multiselect').on('switchChange.bootstrapSwitch',function (event, state) {
var checkBox = document.getElementById("multiselect");
// If the checkbox is checked, display the output text
if (checkBox.checked == true){
document.getElementById("multiselect").value = "1";
} else {
document.getElementById("multiselect").value = "0";
}
});
$('#multiselect').on('switchChange.bootstrapSwitch',函数(事件、状态){
var checkBox=document.getElementById(“multiselect”);
//如果选中该复选框,则显示输出文本
如果(checkBox.checked==true){
document.getElementById(“multiselect”).value=“1”;
}否则{
document.getElementById(“multiselect”).value=“0”;
}
});
它将更新复选框的值。