这是带有复选框
的jsp代码 <div class="col-md-2 right2 fon">
<h6>Clinical Practice/Procedure</h6>
<hr>
<p><input type="hidden" name="clinicalStat" id="clinicalStat" value="0"></p>
<p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalDoc" value="0">Documentation</p>
<p><input type="checkbox" name="clinicalMiss" class="clinicalCheck" id="clinicalDoc" value="0">Missing Files</p>
<p><input type="checkbox" name="clinicalPol" class="clinicalCheck" id="clinicalDoc" value="0" > Policy not available</p>
<p><input type="checkbox" name="clinicalMed" class="clinicalCheck" id="clinicalDoc" value="0" > Medical records unavailable</p>
<p><input type="checkbox" name="clinicalCon" class="clinicalCheck" id="clinicalDoc" value="0" > Confidentiality</p>
<p><input type="checkbox" name="clinicalProc" class="clinicalCheck" id="clinicalDoc" value="0">Procedures not followed</p>
<p><input type="hidden" name="clinicalVals" id="clinicalVals" value="0"></p>
<p><input type="checkbox" id="checkBox" onclick="EnableDisableTextBox(this)" > Other (Specify)</p>
<p><input type="text2" id="text" name="incidentClassClinicalVal" disabled="disabled" style="width: 92%"></p>
</div>
我希望通过提醒获取所选复选框的值。我将该值存储在checked1
中,并且我希望将checked1
的值传递给另一个变量checkedval
。当我尝试alert
checkedval
时,它会向我显示错误"undefined"
复选框的初始值为0
。点击它时如何将其值更改为1
?
这是我的JavaScript代码:
$(":checkbox").click(function()
{
//alert(" you checked");
if(this.checked)
{
var checked1=document.getElementById('clinicalDoc').value;
alert("The value for selected checkbox = "+checked1);
var checkedval=$("#checked1").val('1');
alert("checkedval="+checkedval);
}
});
答案 0 :(得分:3)
id
应为unique
。解决方案是使用this
关键字表示对当前对象的引用。
正如@Alive to Die建议的那样,你应该只使用jQuery或Javascript,完全使用任何一个。
如果您想检查是否选中checkbox
,可以使用 jQuery is
功能。
if($(this).is(':checked')){
//code
}
$(":checkbox").click(function(){
//alert(" you checked");
if($(this).is(':checked'))
{
var checked1=$(this).val();
alert("The value for selected checkbox = "+checked1);
$(this).val('1');
alert("checkedval="+$(this).val());
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2 right2 fon">
<h6>Clinical Practice/Procedure</h6>
<hr>
<p><input type="hidden" name="clinicalStat" id="clinicalStat" value="0"></p>
<p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" value="0">Documentation</p>
<p><input type="checkbox" name="clinicalMiss" class="clinicalCheck"
value="0">Missing Files</p>
<p><input type="checkbox" name="clinicalPol" class="clinicalCheck"
value="0" > Policy not available</p>
<p><input type="checkbox" name="clinicalMed" class="clinicalCheck"
value="0" > Medical records unavailable</p>
<p><input type="checkbox" name="clinicalCon" class="clinicalCheck"
value="0" > Confidentiality</p>
<p><input type="checkbox" name="clinicalProc" class="clinicalCheck"
value="0">Procedures not followed</p>
<p><input type="hidden" name="clinicalVals" id="clinicalVals" value="0"></p>
<p><input type="checkbox" id="checkBox" onclick="EnableDisableTextBox(this)" > Other (Specify)</p>
<p><input type="text2" id="text" name="incidentClassClinicalVal" disabled="disabled" style="width: 92%"></p>
</div>
&#13;