English 中文(简体)
我怎么能对提交表格的所有内容运用同样的活动听众?
原标题:How can I apply the same event listener used for all elements on form submission?

在此,我对《联合材料法典》进行了抽样调查。

function createListener(input) {
  return (e)=> {
    const el=e.target;
    const inputValue=e.target.value;
    const validator=inputCheck(input)
    const valid=validator(inputValue);
    borderHighlight(valid, el)
  }
}

inputs.forEach(input=> {
  input.addEventListener("input", createListener(input))
})


function borderHighlight(valid, el) {
  (valid)? el.style.border= 2px solid green :el.style.border= 2px solid red 
}

myForm.addEventListener( submit , (e)=> {
  e.preventDefault()
  inputs.forEach(input=> {
    createListener(input)
  })
})

每项内容的投入活动聆听者是完美的。 它提供实在错误的信息,作为用户的投入。 但我想,当用户提出形式时(关于形式要素所附的提交活动),这一功能也一样? 我如何在守则中落实这一功能?

最佳回答

在指定职能中确定验证法,以便你能够从两个活动聆听者那里打电话。

function validate_input(el) {
    const inputValue=el.value;
    const validator=inputCheck(el)
    if (validator) {
        const valid= validator(inputValue);
        borderHighlight(valid, el);
    }
}

function createListener(input) {
    return e => validate_input(input);
}

inputs.forEach(input=> {
  input.addEventListener("input", createListener(input))
})


function borderHighlight(valid, el) {
  (valid)? el.style.border= 2px solid green :el.style.border= 2px solid red 
}

myform.addEventListener( submit , (e) => {
  e.preventDefault();
  inputs.forEach(input => validate_input(input));
});
问题回答

Change the form submit listener a bit. Call the function returned from createListener and pass a fake event object: {target: input}.

myForm.addEventListener( submit , (e)=> {
  e.preventDefault()
  inputs.forEach(input=> {
    createListener(input)({target: input});
  })
});

这样,你就不必在<条码>中改动任何内容。





相关问题
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.

热门标签