如何编写javascript来处理复选框并将数据发送到变量?
使用MVC和引导,有一个布尔真/假复选框。但是SQL数据库必须有一个纯文本字段如何编写javascript来处理复选框并将数据发送到变量?,javascript,twitter-bootstrap,checkbox,Javascript,Twitter Bootstrap,Checkbox,使用MVC和引导,有一个布尔真/假复选框。但是SQL数据库必须有一个纯文本字段 input type = checkbox; name = xactive" onclick="setvalue" <script> function(setvalue) if(checkbotive == checked) {model => model.PType1 = "Yes";} else {model => model.PType1 = "No"; </script&
input type = checkbox; name = xactive" onclick="setvalue"
<script> function(setvalue)
if(checkbotive == checked) {model => model.PType1 = "Yes";}
else {model => model.PType1 = "No";
</script>
您应该始终在HTML中引用ID,因为它们根据定义是唯一的。您可以使用document.getElementById获得每个ID的特定HTML标记/项。您还可以使用document.getElementByName引用复选框。问题是document.getElementByName将返回一个数组,因为名称不是唯一的 只需像这样重写整个html:
<input id="checkbox1"
type="checkbox"
name="checkbox1"
onclick="Applealert()" />Apple
<br />
<input id="checkbox2"
type="checkbox"
name="checkbox2"
onclick="Orangealert()" />Orange
<script>
function Applealert()
{
if(document.getElementById("checkbox1").checked == true)
{
alert("You have clicked on Apple.");
}
}
function Orangealert()
{
if(document.getElementById("checkbox2").checked == true)
{
alert("You have clicked on Orange.");
}
}
</script>
我想这把小提琴回答了你的问题 您可以通过其ID获取页面上的元素。这将有助于您处理警报
以下是一个简短的版本:
<input id="checkbox1"
type="checkbox"
name="checkbox1"
onclick="Applealert(this)" />Apple
<br />
<input id="checkbox2"
type="checkbox"
name="checkbox2"
onclick="Orangealert(this)" />Orange
<script>
function Applealert(checkbox)
{
if(checkbox.checked == true)
{
alert("You have clicked on Apple.");
}
}
function Orangealert(checkbox)
{
if(checkbox.checked == true)
{
alert("You have clicked on Orange.");
}
}
</script>
它不应该是document.Form1.checkbox2.checked==true吗?因为checkbox2可能未定义。仅供参考,您可以将一个参数传递给函数,如函数Applealertcheckbox,并使用Applealertthis更改onclick,然后在Applealert函数中,您将能够执行checkbox.checked==True。我相信我不理解您的问题。我可以用我的调试器在每一点上停下来我不知道。我总是使用Chrome开发者工具进行调试。我刚刚用代码更新了上面的答案,试图修改原始模型参数。单击复选框,它应该更新变量,然后才能创建新记录,但创建时,即使model.startdate直接设置为字符串值,也没有数据?
<input id="checkbox1"
type="checkbox"
name="checkbox1"
onclick="Applealert(this)" />Apple
<br />
<input id="checkbox2"
type="checkbox"
name="checkbox2"
onclick="Orangealert(this)" />Orange
<script>
function Applealert(checkbox)
{
if(checkbox.checked == true)
{
alert("You have clicked on Apple.");
}
}
function Orangealert(checkbox)
{
if(checkbox.checked == true)
{
alert("You have clicked on Orange.");
}
}
</script>