价值.长度& lt;10 .不工作返回false

value.length < 10 return false not working

本文关键字:工作 返回 false 长度 lt 价值      更新时间:2023-09-26

如果文本字段不超过10个字符,我使用onsubmit条件返回false,但是它刚才停止提交,我不认为我改变了任何条件,但显然我一定做了什么

<form class="form-new" action="insert/insert.php" method="post" onsubmit="if (document.getElementById('post-input').value.length < 10) return false;">
    <textarea class="form-new" id="post-input" name="text"></textarea><br/>
    <input type="hidden" name="ciudad" value="<?=$city;?>">
    <input type="text" name="colegio" value="<?=$group;?>">
    <input class="form-new" type="submit" value="Publicar"/>
</form>

有办法吗?还是我做错了?由于

这是你的例子。

但是看一下,如果你在onsubmit中返回false,表单无论如何都会被发送。为了执行alert或类似的操作,您应该这样做:

<script type="text/javascript">
    function checkLength() {
        var textarea=document.getElementById('post-input');
    alert(textarea.value.length >= 10);
        if (textarea.value.length >= 10) {
            document.getElementById('myform').submit();
        }
    }
</script>
<form class="form-new" action="insert/insert.php" id="myform" method="post">
    <textarea class="form-new" id="post-input" name="text"></textarea><br/>
    <input type="hidden" name="ciudad" value="<?=$city;?>">
    <input type="text" name="colegio" value="<?=$group;?>">
    <input class="form-new" type="button" value="Publicar" onclick="checkLength()"/>
</form>

,这是链接:http://jsfiddle.net/YNcU4/1/。

UPDATE:请阅读评论。原来的代码应该可以工作