2017-05-08 61 views
1

當我使用函數initSwitchery2時,在選中.js-switch-7時,我無法更改.js-check-change-field-7 forchange事件的文本。切換元素的onchange函數

$(document).ready(function() { 
    handleSwitcheryElements(); 
}); 


function initSwitchery2($class, $color, $speed, $size, $secondarycolor, $class2) { 
    var elems = Array.prototype.slice.call(document.querySelectorAll($class)); 
    var changeFields = Array.prototype.slice.call(document.querySelectorAll($class2)); 
     elems.forEach(function(el) { 
       if ($(el).data('switchery') != true) { 
        new Switchery(el, { color: $color, speed: $speed, size: $size, secondaryColor: $secondarycolor }); 
       } 
      }); 
     elems.onchange = function() { 
       if ($($class).is(':checked')) { 
        changeFields.innerText = "Afficher"; 
        $($class2).removeClass("btn-danger").addClass("btn-success"); 
       } else { 
        changeFields.innerText = "Masquer"; 
        $($class2).removeClass("btn-success").addClass("btn-danger"); 
       } 
      }; 
     } 


handleSwitcheryElements = function() { 
    initSwitchery2('.js-switch-7', '#00ACAC', '0.3s', 'small', '#ff5b57', '.js-check-change-field-7'); 
}; 


--html-- 

<input type="checkbox" data-render="switchery" data-theme="blue" class="js-switch-7 text-right" data-change="check-switchery-state-text" /> 
<a href="#" class="btn btn-xs btn-danger disabled m-l-5 js-check-change-field-7" data-id="switchery-state-text">Masquer</a> 

回答

1

你必須通過設置

elems[0].onchange = function() { 
    //code stuff 
} 

或者使用jQuery使用本地.addEventListener /或.on使用jQuery

所以不是

elems.onchange = function() { 
    ..... 
} 

將JS使用的第一個元素你的活動如下:

$(elems).on("change" , function() { 

     //code stuff 
}); 

如果你想最後這適用於所有的複選框:

添加一個唯一的ID爲每個a元素,並添加這最後的數據屬性爲您輸入

例子a -> id =「a1」它的輸入 - > data-id-target =「a1」等等。

婁一個工作示例:

$(document).ready(function() { 
 
    handleSwitcheryElements(); 
 
}); 
 

 

 
function initSwitchery2($class, $color, $speed, $size, $secondarycolor) { 
 
    var elems = Array.prototype.slice.call(document.querySelectorAll($class)); 
 
    
 
     elems.forEach(function(el) { 
 
       if ($(el).data('switchery') != true) { 
 
        
 
        new Switchery(el, { color: $color, speed: $speed, size: $size, secondaryColor: $secondarycolor }); 
 
        
 
        el.onchange = function(e) { 
 
         if ($(this).is(':checked')) { 
 
          $("#"+$(this).data("id-target")).html("Afficher"); 
 
          $("#"+$(this).data("id-target")).removeClass("btn-danger").addClass("btn-success"); 
 
         } else { 
 
          $("#"+$(this).data("id-target")).html("Masquer"); 
 
          $("#"+$(this).data("id-target")).removeClass("btn-success").addClass("btn-danger"); 
 
         } 
 
        } 
 
        
 
       } 
 
      }); 
 
     } 
 

 

 
handleSwitcheryElements = function() { 
 
    initSwitchery2('.js-switch-7', '#00ACAC', '0.3s', 'small', '#ff5b57'); 
 
    
 
};
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" rel="stylesheet"/> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.js"></script> 
 
<div> 
 
<input type="checkbox" data-render="switchery" data-theme="blue" class="js-switch-7 text-right" data-change="check-switchery-state-text" data-id-target="a1" /> 
 
<a href="#" class="btn btn-xs btn-danger disabled m-l-5 js-check-change-field-7" data-id="switchery-state-text" id="a1">Masquer</a> 
 
</div> 
 
<div> 
 
<input type="checkbox" data-render="switchery" data-theme="blue" class="js-switch-7 text-right" data-change="check-switchery-state-text" data-id-target="a2" /> 
 
<a href="#" class="btn btn-xs btn-danger disabled m-l-5 js-check-change-field-7" data-id="switchery-state-text" id="a2">Masquer</a> 
 
</div> 
 
<div> 
 
<input type="checkbox" data-render="switchery" data-theme="blue" class="js-switch-7 text-right" data-change="check-switchery-state-text" data-id-target="a3" /> 
 
<a href="#" class="btn btn-xs btn-danger disabled m-l-5 js-check-change-field-7" data-id="switchery-state-text" id="a3">Masquer</a> 
 
</div> 
 
<div> 
 
<input type="checkbox" data-render="switchery" data-theme="blue" class="js-switch-7 text-right" data-change="check-switchery-state-text" data-id-target="a4" /> 
 
<a href="#" class="btn btn-xs btn-danger disabled m-l-5 js-check-change-field-7" data-id="switchery-state-text" id="a4">Masquer</a> 
 
</div>

+0

如果我有多個 Masquer,所有文本都會發生變化,檢查時如何爲每個文本做出變化? – space110

+0

嗨@ space110看我編輯的答案,我設法做出多個,如果你想解釋只是要求它:) –

+0

非常感謝幫助! :) – space110