2017-02-08 11 views
0

誰かが私のコードがなぜ機能しないのか教えていただけますか?riot js undefined value

私は "this.usernameが未定義である" と私は本当に理由を理解していない取得...ここ

コード:あなたの助けを

riot.tag2('test', '<input type="text" name="username" placeholder="username" oninput={validate} value="" /> <h4>{username_valid}</h4>', '', '', function(opts) { 
    this.validate = function(e) { 
     this.username_valid = (this.username.value.length > 3) ? 'Valid' : 'Invalid' 
    }; 
}); 

おかげ..

+0

ためoninputイベント

  • 使用this.root.querySelectorAll('input')[0].valueから要素を取得するためにe.target.valueを使用して、REF validate機能で

  • から値を取得するためにthis.refs.username.valueを使用どのRiotバージョンを使用していますか? v3を使用している場合は、refsを使用して変数を参照する必要があります。したがって、入力では、ref = "username"を、this.refs.username.valueのメソッドには、ref = "username"を設定する必要があります。 http://riotjs.com/guide/#named-elements – vitomd

  • 答えて

    3

    だけ読みにくいので、実際にはriot APIを使ってコンポーネントを記述していないことを願っています。私はそれをタグに転記してより良い外観を得ることができます:

    <test> 
    
        <input 
        type="text" 
        name="username" 
        placeholder="username" 
        oninput={validate} 
        value=""/> 
    
        <h4>{username_valid}</h4> 
    
        <script> 
    
        this.validate = function(e) { 
         this.username_valid = (this.username.value.length > 3) ? 'Valid' : 'Invalid' 
        } 
    
        </script> 
    </test> 
    

    入力要素からユーザー名を取得しようとしています。タグ内のthisは、DOM要素ではなくスクリプトのスコープを参照します。あなたは

    1. <input>タグにref='username'を追加入力値を取得することができ、いくつかの方法があります。
      そして、スコープDOMクエリ