2017-04-13 1 views
1

私は、フォーム上で検証を実行するためにAbideを使用していますが、正常に動作しています。しかし、フォームが有効であるか無効である場合、私はアクションを実行するのが難しいです。Foundationを起動する - valid.fndtn.abideを起動する

HTML:

<form role="form" id="form_search_extended" name="form_search_extended" data-abide="ajax" novalidate> 

    <input name="postalCode" type="text" value="{{$data['postalCode']['value']}}" placeholder="ZIP/Postal Code" required maxlength="10"> 

    <button type="submit" value="Submit">Submit</button> 

</form> 

のJavascript:有効または無効リスナー内部

$('#form_search_extended') 
     .on('invalid.fndtn.abide', function() { 
     callFunctionOnValid(); 
     console.log('invalid!'); 
     }) 
     .on('valid.fndtn.abide', function() { 
     console.log('valid!'); 
     }); 

var form = document.getElementById('form_search_extended'); 
form.onsubmit = function(e) { 

    e.preventDefault(); 

    console.log('form submitted'); 
}; 

コードが実行されませんでした。

なぜこれが機能しないのかわかりません。 valid.fndtn.abideのイベントリスナーが起動されていませんか?フィールドの検証が表示されているため、Abideは機能しています。

ありがとうございました。

答えて

1

スクリプトを初期化する必要があります。

終了bodyタグ

$('#form_search_extended') 
 
    .on('valid.fndtn.abide', function(e, el) { 
 
    callFunctionOnValid(); 
 
    console.log('invalid!'); 
 
    }) 
 
    .on('invalid.fndtn.abide', function(e, el) { 
 
    console.log(e.target, 'valid!'); 
 
    }) 
 
    .on("submit", function(ev) { 
 
    ev.preventDefault(); 
 
    console.log("Submitted"); 
 
    });
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/normalize.min.css" rel="stylesheet" type="text/css" /> 
 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" rel="stylesheet" type="text/css" /> 
 
    
 
</head> 
 

 
<body> 
 

 
    <form data-abide novalidate role="form" id="form_search_extended" name="form_search_extended"> 
 
    <label>Number required 
 
    <input name="postalCode" type="text" placeholder="ZIP/Postal Code" required pattern="number" maxlength="10"> 
 
    </label> 
 
    <small class="error">You need a number.</small> 
 
    <button type="submit" value="Submit">Submit</button> 
 
    </form> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/modernizr.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/jquery.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/plugins/foundation.abide.min.js"></script> 
 
    <script> 
 
    $(document).foundation(); 
 
    </script> 
 
</body> 
 

 
</html>

<script> 
    $(document).foundation(); 
    </script> 

を追加します。

関連する問題