复选框选中或取消选中javascript

时间:2018-01-27 17:36:51

标签: javascript html checkbox

我正在尝试检查并查看复选框是选中还是取消选中,但是当我使用element.value时,无论出于何种原因,它总是返回false ...我也尝试使用onclick。我还将元素的值打印到控制台,以查看是否可以判断值是否更改,但无法确定复选框何时为true。任何帮助都很棒!提前致谢!无论出于何种原因,它都会继续进入else块。

HTML

start:   LD $condition  -- Check condition
         JZ else_br     -- Conditional jump
         LD true_val
         RET            -- Return true
else_br: LD false_val
         RET            -- Return false

2 个答案:

答案 0 :(得分:0)

SQL> select department_id, round(avg(salary)) avg_sal, count(*) "# of employees"
  2  from employees
  3  where department_id in (20, 30)
  4  group by department_id
  5  order by 1;

DEPARTMENT_ID    AVG_SAL # of employees
------------- ---------- --------------
           20       9500              2
           30       4150              6

SQL>

在您现有的代码中,我已经从 if(element.value){ if(element.srcElement.checked){及其工作正常。 / p>

希望它有所帮助。

答案 1 :(得分:-1)

<div class="form-check">
   <input class="form-check-input" type="checkbox" value="" onchange="javascript:addFilter(this, event,'freshman');">
   <label class="form-check-label" for="defaultCheck1">Freshman</label>
</div>
<script>
function addFilter(obj, element,string) {

  let filter_array =[];
  if(obj.checked){
  if(string =="freshman"){
    filter_array.push(string);
    console.log("Add Freshman to Array");
  }
  if(string =="sophmore"){
    filter_array.push(string);
    console.log("Add Junior to Array");
  }
  if(string =="junior"){
    filter_array.push(string);
    console.log("Add junior to Array");
  }
  if(string =="senior"){
    filter_array.push(string);
    console.log("Add Senior from to Array");
  }

}

else {

  if(string =="freshman"){
    filter_array.push(string);
    console.log("Remove Freshman from Array");
    console.log(element);
  }
  if(string =="sophmore"){
    filter_array.push(string);
    console.log("Remove Junior from Array");
  }
  if(string =="junior"){
    filter_array.push(string);
    console.log("Remove junior from Array");
  }
  if(string =="senior"){
    filter_array.push(string);
    console.log("Remove Senior from Array");
  }
}
}
</script>

这应该有效

正在使用.checked属性

快乐编码:)