Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 衡量言行_Javascript_Forms - Fatal编程技术网

Javascript 衡量言行

Javascript 衡量言行,javascript,forms,Javascript,Forms,如何用Javascript编写脚本,该脚本将测量表单上输入文本字段的字符数,如果少于2,则在尝试发送表单时将显示警报,如果大于或等于2,则将通过单击表单内部提交按钮中的发送按钮发送表单,考虑以下事项: <button onclick="checkValidity()">Submit</button> 在html主体内的标记中添加以下代码: function checkValidity(){ var val = document.getElementById('

如何用Javascript编写脚本,该脚本将测量表单上输入文本字段的字符数,如果少于2,则在尝试发送表单时将显示警报,如果大于或等于2,则将通过单击表单内部提交按钮中的发送按钮发送表单,考虑以下事项:

<button onclick="checkValidity()">Submit</button>
在html主体内的标记中添加以下代码:

function checkValidity(){
     var val = document.getElementById('inputfield').value; //I'm assuming the id of the input text field to be inputfield
    if(val.length<2) window.alert('too short');

}

在表单中的提交按钮中,考虑执行以下操作:

<button onclick="checkValidity()">Submit</button>
在html主体内的标记中添加以下代码:

function checkValidity(){
     var val = document.getElementById('inputfield').value; //I'm assuming the id of the input text field to be inputfield
    if(val.length<2) window.alert('too short');

}

实现这一目标的方法有很多,包括:

1-向输入元素添加模式属性:


实现这一目标的方法有很多,包括:

1-向输入元素添加模式属性:

另一个简单的方法: HTML:

JS:

不要忘记在代码之前导入jQuery库:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
另一个简单的方法: HTML:

JS:

不要忘记在代码之前导入jQuery库:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>

如果您在一个文本框中进行检查,这将起作用。但是,如果您有一个完整的表单要提交给某个服务器,这并不是最好的方式。如果您在一个文本框中进行检查,这将起作用。但是,如果您有一个完整的表单要提交给某个服务器,这并不是最好的方式。