0

任何字段输入验证的解决方案?如果有人选择第一个选项/值 =“”,则为假。这是我的html:

<div class="fieldinput" id="disney">
                <label for="disney">Disney Character</label>
            <select name="disney">
                <option value="">Choose...</option>
                <option value="mickey">Mickey Mouse</option>
                <option value="mini">Mini Mouse</option>
            </select>
            </div>

我的 javascript

function validate() {
        var errMsg = "";
    var result = true;


    if (disney == "") { 
    errMsg += "An option must be selected.\n"; 
    }

if (errMsg != "") {
        alert (errMsg);
        result = false;
    } 
    return result;
}

有人可以帮忙吗?提前致谢 :)

4

2 回答 2

0

U shd 用途:

var disney= document.getElementsByName('disney')[0].value;
于 2013-11-01T02:03:10.510 回答
0

试试这个: HTML:

<div class="fieldinput" id="disney">
    <label for="disney">Disney Character</label>
    <select name="disney">
        <option value="">Choose...</option>
        <option value="mickey">Mickey Mouse</option>
        <option value="mini">Mini Mouse</option>
    </select>
</div>
<input type="button" value="submit" id="btnSub" />

JS:

function validate(disney) {
    var result = !!disney;
    if (!result) { 
        alert("An option must be selected.\n"); 
    }
    return result;
}

var btnSub = document.getElementById("btnSub");
btnSub.onclick = function(){
    var disney = document.getElementsByName("disney")[0];
    var disVal = disney.options[disney.selectedIndex].value;
    var ret = validate(disVal);
    //ret is validate result
}

如:http: //jsfiddle.net/artwl/XMEwD/1/

于 2013-11-01T02:05:26.220 回答