我有一个复选框,我想在选中时运行一个函数,而在未选中时运行另一个函数。我的代码是:
function showCustomer(str,str2) {
if (document.getElementById(str).checked) {
var xhttp;
if (str == "") {
document.getElementById("xxxx").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("xxxx").innerHTML = this.responseText;
}
};
xhttp.open("GET", "add.jsp?q=" + str + "&q2=" + str2, true);
xhttp.send();
}
else {
var xhttp;
if (str == "") {
document.getElementById("xxxx").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("xxxx").innerHTML = this.responseText;
}
};
xhttp.open("GET", "minus.jsp?q=" + str + "&q2=" + str2, true);
xhttp.send();
}
}
我的复选框代码是:
<input id="<%=cheque_tbl.getPRICE()%>" class="ch" type="checkbox" name="<%=cheque_tbl.getCHECKID()%>" onchange="showCustomer(this.value,this.id)">
问题是那document.getElementById(str).checked
不起作用,必须起作用,document.getElementById("str").checked
但是我的复选框ID不是简单的字符串,而是一个变量。
如何在var中使用var getelement
?为什么我的代码不起作用?
HTML更改:绑定复选框范围以通过onchange函数中的“ this”进行访问
<input id="dd" class="ch" type="checkbox" name="dd" onchange="showCustomer.apply(this,[this.value,this.id])">
JS变更:
function showCustomer(str,str2) {
if (this.checked) {
var xhttp;
if (str == "") {
document.getElementById("xxxx").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("xxxx").innerHTML = this.responseText;
}
};
xhttp.open("GET", "add.jsp?q=" + str + "&q2=" + str2, true);
xhttp.send();
}
else {
var xhttp;
if (str == "") {
document.getElementById("xxxx").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("xxxx").innerHTML = this.responseText;
}
};
xhttp.open("GET", "minus.jsp?q=" + str + "&q2=" + str2, true);
xhttp.send();
}
}
希望它有助于解决该复选框的Document.getElementById()问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句