javascript - Focusing on Radio Buttons - Stack Overflow

Objective: Fix the code below, and understand why focusing on radio buttons doesn't work like focu

Objective: Fix the code below, and understand why focusing on radio buttons doesn't work like focusing on text input boxes.

Question: Why does this not work:

function isValidRadio(elem, helperMsg) {
    var valid = false;
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].checked) {
            return true;
        }
    }
    alert(notice);
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
}

...when this does:

function isValidText(elem, notice) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert(notice);         
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}

Example Code:

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}
function validateForm(form) {
    if (isValidText(form.phone, "Fail."))  {
        if (isValidRadio(form.yesNo, "Fail."))  {
            return true;
        }
        return false;
    }
}
<form id='myForm' action='/formPage/collectinfo/' method='post' accept-charset='UTF-8' name="myForm">
    <input name="phone" type="text" maxlength="14" size="14" />
    <input type="radio" name="yesNo" value="Yes"> Yes <input type="radio" name="yesNo" value="No" > No
<input type="submit" value=" Submit " name="Submit" onclick="return validateForm(myForm)" />

Details: I'm using php/javascript/html and so far I've only been testing this piece in Chrome. The form works just fine and the validation works up until it hits isValidRadio. The alert runs, but then the javascript breaks. A code fix would be great, but I wouldn't mind 'fishing for a life time...'

Objective: Fix the code below, and understand why focusing on radio buttons doesn't work like focusing on text input boxes.

Question: Why does this not work:

function isValidRadio(elem, helperMsg) {
    var valid = false;
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].checked) {
            return true;
        }
    }
    alert(notice);
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
}

...when this does:

function isValidText(elem, notice) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert(notice);         
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}

Example Code:

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}
function validateForm(form) {
    if (isValidText(form.phone, "Fail."))  {
        if (isValidRadio(form.yesNo, "Fail."))  {
            return true;
        }
        return false;
    }
}
<form id='myForm' action='/formPage/collectinfo/' method='post' accept-charset='UTF-8' name="myForm">
    <input name="phone" type="text" maxlength="14" size="14" />
    <input type="radio" name="yesNo" value="Yes"> Yes <input type="radio" name="yesNo" value="No" > No
<input type="submit" value=" Submit " name="Submit" onclick="return validateForm(myForm)" />

Details: I'm using php/javascript/html and so far I've only been testing this piece in Chrome. The form works just fine and the validation works up until it hits isValidRadio. The alert runs, but then the javascript breaks. A code fix would be great, but I wouldn't mind 'fishing for a life time...'

Share Improve this question asked Jan 23, 2013 at 19:43 lostphilosopherlostphilosopher 4,5214 gold badges29 silver badges40 bronze badges 3
  • Please don't eval your setTimeout callbacks. – SeanCannon Commented Jan 23, 2013 at 19:46
  • @AlienWebguy: I started messing with those because they were mentioned as a possible solution to this sort of problem (obviously they didn't work). I'm happy to nix them, but would love a bit more detail on what's wrong with them. (Beyond they are unnecessary, which I assume.) – lostphilosopher Commented Jan 23, 2013 at 19:49
  • 1 It's just frowned-upon syntax to use setTimeout("myfunction()",0); versus setTimeout(function(){ myfunction(); }, 0); – SeanCannon Commented Jan 23, 2013 at 20:32
Add a ment  | 

1 Answer 1

Reset to default 2

Alright, I found the solution, AlienWebguy put me on the right track thinking about arrays. I just needed to specify which radio button to focus on as opposed to which set. Here is the working code:

// Validate that the user has checked one of the radio buttons
function isValidRadio(radio, helperMsg) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert(helperMsg);
    radio[0].focus();
    return false;
}

Called via: isValidRadio(form.yesNo, "Error.") Thanks for the assist.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249746a4618604.html

相关推荐

  • javascript - Focusing on Radio Buttons - Stack Overflow

    Objective: Fix the code below, and understand why focusing on radio buttons doesn't work like focu

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信