[英][JavaScript]: How to define a variable as object type?
I am using following code to check whether a check box on my website page is checked or not. 我正在使用以下代码检查我的网站页面上的复选框是否已选中。 But there are several check boxes and I want to use this same function.
但是有几个复选框,我想使用相同的功能。 I want to call this function from a Submit button click and pass the check box name as argument.
我想通过单击“提交”按钮来调用此函数,并将复选框名称作为参数传递。 It should than validate that check box.
然后应该验证该复选框。
function CheckTermsAcceptance()
{
try
{
if (!document.getElementById('chkStudent').checked)
alert("You need to accept the terms by checking the box.")
return false;
}
catch(err)
{
alert(err.description);
}
}
Just pass a parameter to CheckTermsAcceptance()
. 只需将参数传递给
CheckTermsAcceptance()
。 You also missed a brace after the alert -- there are two statements in that if
block, and you'll always execute the return false
without it. 您还错过了警报后的括号-在
if
块中有两个语句,并且始终没有它就执行return false
。
function CheckTermsAcceptance(checkboxName)
{
try
{
if (!document.getElementById(checkboxName).checked) {
alert("You need to accept the terms by checking the box.")
return false;
}
}
catch(err)
{
alert(err.description);
}
}
To call this from your submit button, have a function like validateForm
that's called on submit. 要从您的提交按钮调用此函数,请具有一个诸如
validateForm
之类的函数,该函数在Submit时调用。 Then simply construct a list of the checkboxes and pass in their IDs to CheckTermsAcceptance
. 然后,只需构造复选框列表,并将其ID传递给
CheckTermsAcceptance
。
Note that this sort of validation is handled very smoothly by jQuery and its ilk. 请注意,jQuery及其类似功能可以非常顺利地处理这种验证。 For example, here's the jQuery validation plugin.
例如, 这是 jQuery验证插件。
function CheckTermsAcceptance(element){
try{
if (!element.checked){
alert("You need to accept the terms by checking the box.")
return false;
}
}catch(err){
alert(err.description);
}
}
and you call it like: 你这样称呼:
CheckTermsAcceptance(document.getElementById('chkStudent'));
is that it? 是吗
Sorry for not answering your questions. 很抱歉没有回答您的问题。 But you should seriously consider using jQuery and jQuery validate .
但是您应该认真考虑使用jQuery和jQuery validate 。
this function will work with a bit of fix up, pass argument and make sure you do both the alert and the return false in the if statement 此函数将进行一些修复,传递参数,并确保在if语句中同时执行警报和返回false
function CheckTermsAcceptance(checkBox) //added argument
{
try
{
if (!checkBox.checked) { //added block to group alert and fail case
alert("You need to accept the terms by checking the box.")
return false;
}
return true; //added success case
}
catch(err)
{
alert(err.description);
}
}
once you have this in place you can then use it on your form validation like so 将其放置到位后,您就可以像这样在表单验证中使用它
<form id="formid" action="" onsubmit="return validate('formid');">
<input type=checkbox name="name" id="name"><label for="name">Name</label>
<input type=checkbox name="name2" id="name2"><label for="name2">Name2</label>
<input type=submit>
</form>
<script>
function validate(formid) {
var form = document.getElementById(formid);
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements[i];
if (elem.type == 'checkbox' && !CheckTermsAcceptance(elem)) {
return false;
}
}
return true;
}
</script>
i can confirm that this works in firefox 3.5 我可以确认这在Firefox 3.5中有效
also jQuery and jQuery.validate make this very easy to implement in a very declarative way. jQuery和jQuery.validate也使它很容易以声明性的方式实现。
You could also use more arguments to allow for different options as well. 您还可以使用更多参数来允许不同的选项。
function CheckTermsAcceptance()
{
var ctrl = arguments[0];
var valueExpected = arguments[1];
var outputMessage = arguments[2];
if(valueExpected == null) valueExpected = true;
if(outputMessage == null) outputMessage = "You need to accept the terms by checking the box.";
try
{
if(ctrl.checked == valueExpected)
{
Log.Message(outputMessage);
}
}
catch(err)
{
alert(err.description);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.