弹出窗体的字段空错误

时间:2013-07-08 11:52:38

标签: javascript

我可以请求帮助请求,我为弹出窗口创建验证字段:

 function prepareEventHandlers() {
document.getElementById("contact").onsubmit = function () {
if (document.getElementById("message").value == '') {
    document.getElementById("errorMessage").innerHTML = 'the field should not be empty!';
    return false;
}
else {
    document.getElementById("errorMessage").innerHTML = '';
    return true;
}
};
}


window.onload = function () {
prepareEventHandlers();
}

然后是html代码:

<div id="form-content" class="modal hide fade in" style="display: none;">

        <div class="modal-body">
            <form class="contact" name="contact" >
                <label class="label" for="message">Enter a Message</label><br>
                                    <textarea id="message" name="message" class="input-xlarge"></textarea>             
                                    <p><span id="errorMessage"></span></p>
            </form>
        </div>
        <div class="modal-footer">
            <input class="btn btn-success" type="submit" value="Send!" id="btnsubmit">
            <a href="#" class="btn" data-dismiss="modal">No!</a>
        </div>

我收到了这个错误:

TypeError: document.getElementById(...) is null document.getElementById("contact").onsubmit = function () {

任何想法?

修改

好的,我将id="contact"添加到我的表单中,以便错误消失,但现在弹出窗体是显示的,但是当我尝试单击send为空或非空值时,没有发生任何事情......

1 个答案:

答案 0 :(得分:1)

</form>

之后关闭<input class="btn btn-success" type="submit" value="Send!" id="btnsubmit">

并将html表单ID更改为contact