Javascript checkbox onChange

后端 未结 10 1235
孤街浪徒
孤街浪徒 2020-11-28 06:00

I have a checkbox in a form and I\'d like it to work according to following scenario:

  • if someone checks it, the value of a textfield (totalCost)
10条回答
  •  无人及你
    2020-11-28 06:42

    Reference the checkbox by it's id and not with the # Assign the function to the onclick attribute rather than using the change attribute

    var checkbox = $("save_" + fieldName);
    checkbox.onclick = function(event) {
        var checkbox = event.target;
    
        if (checkbox.checked) {
            //Checkbox has been checked
        } else {
            //Checkbox has been unchecked
        }
    };
    

提交回复
热议问题