English 中文(简体)
javascript Add.ventListenerfire on all elements
原标题:javascript addEventListener firing on all elements

Im sure I have missed something obvious but any idea why the following addEventListener code is firing everywhere (on not just on the submit button)?

HTML:

    <form action="#">
        <input type="text"><br>
        <input type="text"><br>
        <button id="submit">submit</button><br>
    </form>

JS:

    function validate () {
        var inputs = document.getElementsByTagName( input );
        var inputs_length = inputs.length-1;
        for (i=0; i<=inputs_length; i++) {
            var inputs_value = document.getElementsByTagName( input )[i].value;
            if (inputs_value == "") {
                alert( there is a text box empty );
            }
        }

    }   

    var el = document.getElementById( submit );

    if (el.addEventListener) {  
        el = addEventListener( click , validate, false);   
    } else if (el.attachEvent)  {  
      el.attachEvent( onclick , validate);  
    } 

缩略语

el = addEventListener( click , validate, false); 

目 录

el.addEventListener( click , validate, false); 

MY TYPO:

最佳回答

修改:

if (el.addEventListener) {  
    el = addEventListener( click , validate, false);  

为此:

if (el.addEventListener) {  
    el.addEventListener( click , validate, false);  
问题回答

冗长的评论。

在您的法典中:

    // inputs is an HTMLCollection of all the inputs
    var inputs = document.getElementsByTagName( input );

    // No need for the -1
    var inputs_length = inputs.length;

    // Don t forget to declare counters too, very important
    // Just use < length
    for (var i=0; i<inputs_length; i++) {

        // Instead of this very inefficient method
        var inputs_value = document.getElementsByTagName( input )[i].value;

        // use the collection you already have
        var inputs_value = inputs[i].value;

        if (inputs_value == "") {
            alert( there is a text box empty );
        }
    }

最好在开始时宣布<代码>投入-价值<>代码>,这样所有申报都放在一个地方,但因为你没有有害。

Oh,没有给出一个形式要素,即“提交书”(或任何其他形式)的名称或背书,因为它将给同一名称的形式方法蒙上阴影,因此可以被称作。





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

热门标签