如何在选中复选框时调用javascript函数

时间:2016-10-15 05:35:55

标签: javascript html

我试过这个:

    function stickyheaddsadaer() {
        $("#page-header-inner").addClass("sticky");
    }

    <input type="checkbox" name="TT_sticky_header" id="TT_sticky_header_function" 
value="{TT_sticky_header}" onclick="stickyheaddsadaer()"/>

所以当我点击复选框时,它没有任何反复......

但是当我尝试这个时:

function stickyheaddsadaer() {
    alert("I am an alert box!");
}

然后工作......

你能帮帮我吗?我需要通过复选框激活javasript功能

当js函数处于活动状态时,它会将类添加到div

谢谢

3 个答案:

答案 0 :(得分:6)

最好使用onchange事件并检查内部功能(如果已选中)

&#13;
&#13;
function stickyheaddsadaer(obj) {
  if($(obj).is(":checked")){
    alert("Yes checked"); //when checked
    $("#page-header-inner").addClass("sticky");
  }else{
    alert("Not checked"); //when not checked
  }
  
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="TT_sticky_header" id="TT_sticky_header_function" value="{TT_sticky_header}" onchange="stickyheaddsadaer(this)"/>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

HTML

<input type="checkbox" id="switch" onclick="change()">

JS

 function change() {
var decider = document.getElementById('switch');

if(decider.checked){
  alert('check')
}else{
  alert('unchecked')
}

答案 2 :(得分:0)

Dim xlcon As ADODB.Connection
Dim currentDataFilePath As String

xlcon.Open "Driver=Microsoft Access Text Driver (*.txt, *.csv)" & _
      ";Dbq=" & currentDataFilePath  & _
      ";Extensions=asc,csv,tab,txt;"
function checkFluency(){
    var checkbox = document.getElementById('fluency');
    if (checkbox.checked != false) {
	    alert("Checkbox checked")
    }else{
	    alert("Not Checked")
    }
}