2015-02-24 68 views
0

我需要在表单上触发一个提交,然后用javascript处理它。这是组件的样子如何以正确的方式触发提交表单?

var CommentForm = React.createClass({ 
    handleSubmit: function(e) { 
    alert("YES again"); 
    return; 
    }, 
    render: function() { 
    return (
     <div className="postCon"> 
      <form className="commentForm" onSubmit={this.handleSubmit}> 
      <textarea className="textA input" placeholder="Your comment" ref="author" /> 
      </form> 
     </div> 
    ); 
    } 
}) 

而且这是supose触发提交正确的jQuery的反应方法

<script> 
     $(function(){ 
      $('.textA').autosize(); 
     }); 
     $('.textA').keypress(function (e) { 
      if (e.which == 13 && !event.shiftKey) { 
       e.preventDefault(); 
       $(this).closest("form").submit(); 
       return false; //prevent duplicate submission 
      } 
     }); 
    </script> 

的问题是,表格将被发送和handleSubmit永远不会被触发?这是做这件事的正确方式还是我过分复杂?

回答

0

使用键盘事件从阵营 - http://facebook.github.io/react/docs/events.html 例如:

var CommentForm = React.createClass({ 
    handleSubmit: function(e) { 
    alert("YES again"); 
    return; 
    }, 
    handleKeyPress: function(e){console.log(e.which);if(e.which == 13){this.handleSubmit();}}, 
    render: function() { 
    return (
     <div className="postCon"> 
      <form className="commentForm" onSubmit={this.handleSubmit}> 
      <textarea onKeyPress={this.handleKeyPress} className="textA input" placeholder="Your comment" ref="author" /> 
      </form> 
     </div> 
    ); 
    } 
}) 
+1

我想补充这一点,而不是发布一个新的答案:调用形式的'提交()'方法直接不会触发它的'提交“事件,这就是为什么你应该使用上述技术。 – 2015-02-25 01:24:38

相关问题