我有一个form.When,我提交了表单,表单文本字段的require属性应该设置为the。
<html>
<head>
<script>
function validate()
{
alert("ok");
document.getElementById("sample").required = true;//method-1
document.getElementById("sample").setAttribute("required","")//method-2
}
</script>
</head>
<body>
<form method="post" action="" onsubmit="validate()">
<input type="text" id="sample">
<input type="submit" value="submit">
</form>
</body>
</html>在这两种方法中,我都无法将所需设置为true.any帮助,请.
发布于 2018-03-31 12:26:42
您可以像下面的示例一样返回表单提交。这是参考url。
<form name="myForm" action="/action_page.php"onsubmit="return validateForm()" method="post">参见Demo
用element.required = true;
function validate() {
var el = document.getElementById("sample");
if (el.value) {
alert(el.value);
return true;
} else {
alert('Value is required..');
el.required = true; //method-1
return false;
}
}<form method="post" action="#" onsubmit="return validate()">
<input type="text" id="sample">
<input type="submit" value="submit">
</form>
使用element.setAttribute(“必需”、"");
function validate() {
var el = document.getElementById("sample");
if (el.value) {
alert(el.value);
} else {
alert('Value is required..')
el.setAttribute("required", "");
return false;
}
}<form method="post" action="" onsubmit="return validate()">
<input type="text" id="sample">
<input type="submit" value="submit">
</form>
https://stackoverflow.com/questions/49587547
复制相似问题