如何改变点击输入的价值? [英] How to change value of input on click?
本文介绍了如何改变点击输入的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过ajax加载内容,并希望更改每个复选框的值,如果他们点击。我试着
I load content via ajax and want to change value of each checkbox if click of them. I try
function calculate(){
if($('input[type=checkbox][checked]')) {
$(this).attr({
value: 1
});
} else {
$(this).attr({
value: 0
});
}
calculatebbb();
}
并添加到每个输入:
<input type="checkbox" name="a003" value="230073" onclick="calculate()">
但它不检查此元素,它包含所有元素。
but it doesn't check this elemnt, it chcked all elements.
推荐答案
这个工作方式与您一样
HTML
HTML
<input type="checkbox" name="a003" value="230073" onclick="calculate(this);">
Javascript
function calculate(checkbox){
console.log($(checkbox));
if($(checkbox).attr("checked")) {
$(checkbox).val(1);
} else {
$(checkbox).val(0);
}
calculatebbb();
}
您做错了什么:
此
不是您的输入。如果你使用 console.log(this)
this
is not your input here. If you check with console.log(this)
.
复选框] [已选中] 未选择您的输入,也许在此处使用标识。
Also input[type=checkbox][checked]
does not select your input, maybe use an id here.
您可以使用 .val(1)
这篇关于如何改变点击输入的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文