我有3个具有相同ID的复选框。当我尝试在javascript中执行以下操作时,我得到输出为9.你能看到我哪里出错吗?
点击复选框;我叫一个函数 - > chkBoxCount(本);
function chkBoxCount(chk){
alert(chk.id.length;}
编辑:
<input type="checkbox" name="f_seizure" id="f_seizure_1" onclick="chkBoxCount(this);" value="0"> No known episode
<input type="checkbox" name="f_seizure" id="f_seizure_2" onclick="chkBoxCount(this);" value="1"> EEG
<input type="checkbox" name="f_seizure" id="f_seizure_3" onclick="chkBoxCount(this);" value="2"> Suspected/Clinical
答案 0 :(得分:0)
应该是:
<input type="checkbox" name="vehicle" value="Bike" id="bike" onclick="chkBoxCount(this)" > I have a bike</input>
function chkBoxCount(chk){
var id = chk.id;
id = id.split("");
alert(id.length);
}
这样做是获取传递元素的id,将其子串到单个字符,然后提醒字符数。这已经过测试;有用!希望这有帮助!
答案 1 :(得分:0)
你说你期望3结果,所以如果我理解,你想要计算具有相同名称的复选框的数量。这是怎么做的,享受^^
chkBoxCount = function() {
alert($("input[name=f_seizure]").length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="f_seizure" id="f_seizure_1" onclick="chkBoxCount();" value="0"> No known episode
<input type="checkbox" name="f_seizure" id="f_seizure_2" onclick="chkBoxCount();" value="1"> EEG
<input type="checkbox" name="f_seizure" id="f_seizure_3" onclick="chkBoxCount();" value="2"> Suspected/Clinical