And*_*Fox 5 html javascript checkbox
我有一个小html form的三个复选框,每个复选一个row.我希望javascript confirm box在用户取消选中该框时显示,然后如果用户选择取消,则checkbox检查保留.我搜索过这个网站以及其他一些网站,但几乎所有内容都与checkbox被检查有关,就好像他们必须同意某些条款或内容一样.我很新,Javascript但是我尝试根据我应该看起来的样子来制作一个功能,但它不起作用.
这是我的表格:
<form action="" method="post">
<table id="table">
<tr>
<td>Checkbox One</td>
<td align="center">
<input type="checkbox" name="check1" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr>
<td>Checkbox Two</td>
<td align="center">
<input type="checkbox" name="check2" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr>
<td>Checkbox Three</td>
<td align="center">
<input type="checkbox" name="check3" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="submit"></input>
</td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的功能,但不起作用:
function cTrig(name) {
if (name.checked == true) {
confirm("Are you sure you want to do this?");
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个jsfiddle
我希望有一些内容,Javascript因为我想在进入之前更熟悉那种语言jquery,但如果必须这样做jquery,那就没关系了.干杯.
试试这个方式
<form action="" method="post">
<table id="table">
<tr>
<td>Checkbox One</td>
<td align="center">
<input type="checkbox" name ="check1" id="check1" checked='checked' onchange="cTrig('check1')"></input>
</td>
</tr>
<tr>
<td>Checkbox Two</td>
<td align="center">
<input type="checkbox" name="check2" id="check2" checked='checked' onchange="cTrig('check2')"></input>
</td>
</tr>
<tr>
<td>Checkbox Three</td>
<td align="center">
<input type="checkbox" id="check3" name="check3" checked='checked' onchange="cTrig('check3')"></input>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="submit"></input>
</td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
通过使用元素ID
function cTrig(clickedid) {
if (document.getElementById(clickedid).checked == true) {
return false;
} else {
var box= confirm("Are you sure you want to do this?");
if (box==true)
return true;
else
document.getElementById(clickedid).checked = true;
}
}
Run Code Online (Sandbox Code Playgroud)
使用名称
function cTrig(clickedid) {
if (document.getElementsByName(clickedid)[0].checked == true) {
return false;
} else {
var box= confirm("Are you sure you want to do this?");
if (box==true)
return true;
else
document.getElementsByName(clickedid)[0].checked = true;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我会这样:
您的表格:
<form action="" method="post">
<table id="table">
<tr>
<td>Checkbox One</td>
<td align="center">
<input type="checkbox" name="check1" checked="checked" onchange="cTrig(this)"></input>
</td>
</tr>
<tr>
<td>Checkbox Two</td>
<td align="center">
<input type="checkbox" name="check2" checked="checked" onchange="cTrig(this)"></input>
</td>
</tr>
<tr>
<td>Checkbox Three</td>
<td align="center">
<input type="checkbox" name="check3" checked="checked" onchange="cTrig(this)"></input>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="submit"></input>
</td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
JavaScript:
function cTrig(box) {
if (!box.checked) {
if (!confirm("Are you sure you want to do this?")) {
box.checked = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)