【问题标题】:How do you set default checked value on switchery js input tag using ajax?如何使用 ajax 在 switchery js 输入标签上设置默认检查值?
【发布时间】:2019-07-18 03:37:02
【问题描述】:

我正在尝试使用带有 ajax 的数据库中的值为带有 switchery 插件的输入标签设置默认值,但是选中的属性不会更新,但是在没有插件的情况下更新确实有效,我做错了什么? HTML 表格看起来像这样:

<table>
<thead>
...
</thead>
<tbody>
    @for($j=1; $j<=10; $j++)
    ...
    <td>
      <label>
        <input id="day1-hours{{$j}}" type="checkbox" data-plugin="switchery">
        <span>Active</span>
      </label>
    </td>
    ...
    @endfor
</tbody>
</table>

这里是ajax成功函数:

for(let $i=1; $i<=6; $i++){
$.ajax({
... //the POST REQUEST
success:function(data){
    for($j=1; $j<=array.length;$j++){
        if(data[$j-1] == "active"){
             $("#day"+$i+"-hours"+$j).prop("checked",true);

        }else{
             $("#day"+$i+"-hours"+$j).prop("checked",false);                 
        }
     };
   }
 ...
}

来自 ajax 成功响应的数据应该只是“活动”和“非活动”

【问题讨论】:

  • 设置switchery后需要查看它的类。提供:1) 完整的ajax 查询,之前定义了变量,因为查看array$i 变量很有趣; 2) 转到F12 并显示要更改其视图的键input 的图片; 3) 当你搜索$i&$j 时,为什么你的inputX&Y4) $i 的值是多少?
  • @AksenP X 和 Y 的值也从数据库中循环,我没有提供所有代码,因为我认为它与这个问题无关,这些代码在常规复选框输入上按预期工作,但是当我使用切换器时它不会更新,
  • 但是,你需要解释你的秘密$iXY。因为你在 ID 中使用了CHARNUMBER,如果有0..n 而不是字符,可以,但是你呈现字符。
  • 如果你能做到我要求的一切,那么我可以帮助你。在这种情况下,我认为不是唯一的我会。您的plugin 更改input 元素,我需要看看如何。可以通过F12查看。提供两种状态:ONOFF
  • @AksenP 我更新了信息

标签: jquery ajax switchery


【解决方案1】:

使用以下代码。它对我有用。

HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.min.css">

<td>
    <label>
        <input id="dayX-hoursY" type="checkbox" data-plugin="switchery" onclick="ch_label(this)">
        <span>Inactive</span>
    </label> 
</td>
success:function(){
     for($j=1; $j<=array.length;$j++){
        if(data[$j-1] == "active"){

    var css1 = 'background-color: rgb(100, 189, 99); border-color: rgb(100, 189, 99); box-shadow: rgb(100, 189, 99) 0px 0px 0px 16px inset; transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s, background-color 1.2s ease 0s;';
    var css2 = 'left: 20px; transition: background-color 0.4s ease 0s, left 0.2s ease 0s; background-color: rgb(255, 255, 255);'; 

             $("#day"+$i+"-hours"+$j).attr('checked',true);
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(0).attr('style', css1);
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(0).find('small').attr('style',css2);
             var init = new Switchery($("#day"+$i+"-hours"+$j));
             $("#day"+$i+"-hours"+$j).prop('checked', true);
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(1).text('Active'); 
        }else{

    var css3 = 'background-color: rgb(255, 255, 255); border-color: rgb(223, 223, 223); box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s;';
    var css4 = 'left: 0px; transition: background-color 0.4s ease 0s, left 0.2s ease 0s;'; 

             $("#day"+$i+"-hours"+$j).attr('checked',false);
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(0).attr('style',css3);
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(0).find('small').attr('style',css4);
             var init = new Switchery($("#day"+$i+"-hours"+$j));
             $("#day"+$i+"-hours"+$j).prop('checked', false); 
             $("#day"+$i+"-hours"+$j).parent().find('span').eq(1).text('Inactive');               
        }
     };
 }

function ch_label(btn){

   btn.checked == true ? 
            btn.parentNode.children[2].firstChild.nodeValue = 'Active' : 
            btn.parentNode.children[2].firstChild.nodeValue = 'Inactive';
}

我的例子 HTML

<td>
    <label>
        <input id="day1-hours" type="checkbox" data-plugin="switchery" onclick="ch_label(this)">
        <span>Inactive</span>
    </label> 
</td>

<button id="btn">Run</button>

<td>
    <label>
        <input id="day2-hours" type="checkbox" data-plugin="switchery" checked onclick="ch_label(this)">
        <span>Active</span>
    </label> 
</td>

<button id="btn2">Run</button>

JS

var elem = document.querySelector('#day1-hours');
var init = new Switchery(elem);

var elem2 = document.querySelector('#day2-hours');
var init2 = new Switchery(elem2);

$('#btn').click(function(){

    var css1 = 'background-color: rgb(100, 189, 99); border-color: rgb(100, 189, 99); box-shadow: rgb(100, 189, 99) 0px 0px 0px 16px inset; transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s, background-color 1.2s ease 0s;';
    var css2 = 'left: 20px; transition: background-color 0.4s ease 0s, left 0.2s ease 0s; background-color: rgb(255, 255, 255);'; 

    $('#day1-hours').attr('checked',true);
    $('#day1-hours').parent().find('span').eq(0).attr('style', css1);
    $('#day1-hours').parent().find('span').eq(0).find('small').attr('style',css2);
    var init = new Switchery($('#day1-hours'));
    $('#day1-hours').prop('checked', true);
    $('#day1-hours').parent().find('span').eq(1).html('Active');
});

$('#btn2').click(function(){

    var css3 = 'background-color: rgb(255, 255, 255); border-color: rgb(223, 223, 223); box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; transition: border 0.4s ease 0s, box-shadow 0.4s ease 0s;';
    var css4 = 'left: 0px; transition: background-color 0.4s ease 0s, left 0.2s ease 0s;'; 

    $('#day2-hours').attr('checked',false);
    $('#day2-hours').parent().find('span').eq(0).attr('style',css3);
    $('#day2-hours').parent().find('span').eq(0).find('small').attr('style',css4); 
    var init = new Switchery($('#day2-hours'));
    $('#day2-hours').prop('checked', false); 
    $('#day2-hours').parent().find('span').eq(1).html('Inactive');
});

我正在添加和删除 checked 属性,更改 css 样式并重新初始化插件。

检查一下

注意:您可以预先定义您的css,我使用的是默认样式。在if statement 中进行操作。

【讨论】:

  • 它对我不起作用,先生,由于click() 功能,它会在启动时将所有复选框变为活动状态并更改所有值
  • 您的input 是否放置在&lt;td&gt; 标记中并且在此标记中是单独的?我发现了一个错误,答案已更新。给我看你&lt;td&gt;的全部内容。尝试删除click()。我想删除它,prop 已修复它。
  • 对新解决方案的更改和 switchify 没有加载(它加载基本复选框),我刚刚更新了代码,里面只有 1 个 tr 和 3 个 td,最后一个包含输入复选框,感谢您抽出宝贵时间了解此问题
  • 根据您的&lt;td&gt; 内容,代码已稍作更新。根据您的基本复选框-从答案中添加到view cdn 的顶部,您看到了什么?
  • 我已经在先生之前添加了这些依赖项,切换器已正确加载(css和脚本)它只是仍然无法从ajax设置的默认值。当我尝试您的解决方案时,切换器会回到基本复选框
猜你喜欢
  • 2022-10-14
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
相关资源
最近更新 更多