English 中文(简体)
尽管Vanilla Javascript返回,但表格仍在提交。
原标题:Form is still submitting despite returning false Vanilla Javascript

Here is my App.js

errorMessage=(validator, input, value)=> {
  const valid=validator(value)
  if (value==="") {
    input.nextElementSibling.style.display="inherit"
    return false 
  } else if (!valid && value!=="") {
    input.nextElementSibling.nextElementSibling.style.display="inherit"
    return false 
  }
  return true
}

const checkedCheckboxes=Array.from(checkboxes).filter(checkbox=> checkbox.checked);

validateCheckboxes=()=> {
  const checked=checkedCheckboxes.length;
  if (!checked) {
    fieldSetActivity.nextElementSibling.style.display="block"
    return false 
  }
  return true 
}



validateForm=()=> {
  if (!validateCheckboxes()) {
    return false 
  }
  return true
}

// validateForm=()=> {
//   Array.from(inputElements).forEach(element=> {
//     let value=element.value;
//     let input=element;
//     if (input===name) {
//       errorMessage(isValidName, input, value)
//     } else if (element===email) {
//       errorMessage(isValidEmail, input, value);
//     } else if (element===creditNumber) {
//       errorMessage(isValidCreditCard, input, value);
//     } else if (element===zip) {
//       errorMessage(isValidZipCode, input, value);
//     } else if (element===cvv) {
//       errorMessage(isValidCvv, input, value);
//     } else if (element==fieldSetActivity) {
//       validateCheckboxes()
//     }
//   });
// }


form.addEventListener( submit , validateForm);

我研究了与这一问题类似的答案,并且评论了法典,我意识到了母子的bo子。

因此,我决定测试一种功能,即validateCheck Boxes功能,并采用一种以本人形式提交手递的可行解决办法。 但是,尽管检查箱的验证功能恢复不实,但表格仍未提交。

为什么发生这种情况?

最佳回答

http://www.un.org。

你们的法典中有两条内容有待确定。

  1. Returning true/false works if you have an onSubmit=return fun() in your <form> attribute. Since you are using a listener, you need to do event.preventDefault() from the validation function
  2. Your checkedCheckboxes is a const which means it will not be populated dynamically when the checkboxes are checked/unchecked. Move it into your checkbox validation method and make it a local variable using let

在作出这些修改后,你的认证范围如下:

validateCheckboxes=()=> {
// 2. Local variable
let checkedCheckboxes=Array.from(checkboxes).filter(checkbox=> checkbox.checked);
  const checked=checkedCheckboxes.length;
  if (!checked) {
    fieldSetActivity.nextElementSibling.style.display="block"
    return false;
  }
  return true;
}

validateForm=()=> {
  if (!validateCheckboxes()) {
  // 1. preventDefault instead of  false 
     event.preventDefault();
  }
  return;
}

JSFiddle:

OLD ANSWER:

You need to call your JS validation before the form is submitted To do that, use the form s onSubmit attribute like this

<form action="index.html" method="post" onSubmit="return validateForm()">

JSFiddle:

您也可以使用形式活动听众和电话event.preventDefault(),具体载于

引自该网页:

JS:

function logSubmit(event) {
  log.textContent = `Form Submitted! Time stamp: ${event.timeStamp}`;
  event.preventDefault();
}

const form = document.getElementById( form );
const log = document.getElementById( log );
form.addEventListener( submit , logSubmit);

HTML:

<form id="form">
  <label>Test field: <input type="text"></label>
  <br><br>
  <button type="submit">Submit form</button>
</form>
<p id="log"></p>
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签