2015-05-26 49 views
2

所以我正在做表單驗證,並且表單是使用React.js創建的。現在我正在尋找使用jQuery進行表單驗證,但是當我嘗試選擇react.js創建的元素,特別是具有默認值的輸入時,它表示該元素的值未定義。這意味着它沒有被發現。我已經在渲染後運行jQuery,所以我不知道是什麼阻止它找到id。爲什麼jQuery不能找到這個react.js元素?

的index.html

<section id="formDiv" class="sections"> 

</section> 
<script src="js/jquery.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
<!-- React.js --> 
<script src="https://fb.me/react-0.13.3.js"></script> 
<script src="https://fb.me/JSXTransformer-0.13.3.js"></script> 

<!-- Form jsx --> 
<script src="comp/Form.jsx" type="text/jsx"></script> 
<script src="comp/InputGroup.jsx" type="text/jsx"></script> 
<script src="comp/CheckboxGroup.jsx" type="text/jsx"></script> 
<script src="comp/RadioGroup.jsx" type="text/jsx"></script> 
<script type="text/jsx"> 

    React.render(<Form />, document.getElementById('formDiv')); 

</script> 
<script src="js/formverification.js" type="text/javascript"></script> 

Form.jsx(是的,我知道的形式是一個可怕的名字)

var Form = React.createClass({ 

render: function() { 
    return(
     <div className="container form-border"> 
      <h2>Enter your info to subscribe.</h2> 
      <form> 
       <InputGroup className="has-success" for="nameInput" id="nameInput" type="text" placeholder="Name" glyph="glyphicon-ok">Name</InputGroup> 
       <InputGroup className="has-warning" for="emailInput" id="emailInput" type="email" placeholder="Enter email" glyph="glyphicon-warning-sign">Email</InputGroup> 
       <InputGroup className="has-error" for="passInput" id="passInput" type="password" placeholder="Password" glyph="glyphicon-remove">Password</InputGroup> 
       <div className="form-group"> 
        <label for="comments">Comments</label> 
        <textarea className="form-control" id="commentInput" rows="3" placeholder="Enter comments here"></textarea> 
       </div> 
      </form> 
     </div> 
    );   
    } 
}); 

formverification.js

console.log($('#nameInput').val()); 

爲什麼控制檯日誌未定義?

編輯:

對不起,忘了你可能需要這樣:

InputGroup.jsx

var InputGroup = React.createClass({ 
render: function() { 
    return(
     <div className={this.props.className + " form-group has-feedback"}> 
       <label className="control-label" for={this.props.for}>{this.props.children}</label> 
       <input type={this.props.type} value="Hello world" className="form-control" id={this.props.id} placeholder={this.props.placeholder} /> 
       <span className={"glyphicon " + this.props.glyph + " form-control-feedback"}></span> 
     </div> 
     ); 
    } 
}); 
+0

我沒有使用過反應,但如果它呈現異步,那麼你的console.log原本可以得到的InputGroup標籤運行(又名VAL()是不確定的)。嘗試記錄選擇器而不是val()以查看它是否選擇了您期望的內容 –

+1

Object {context:HTMLDocument→index.html,selector:「#nameInput」} 這就是我得到的 –

+0

我不知道是否這就是應該顯示或不,雖然 –

回答

1

發現的問題! JSX Transformer對於內容加載時間太慢,jQuery無法找到它。將jsx編譯成js解決了這個問題,現在彈出了正確的值。

+0

如果是時間問題,則反應可能會發出類似於DOMContentLoaded的事件,除非解析完所有可以收聽的事件。這樣您就不需要轉換或希望時間在100%的時間內保持一致 –

0

你也可以使用React.findDOMNode,因爲如果你遇到了計時問題(等待反應加載),你可能想要在虛擬生命中獲取元素。

看看這個:working-with-the-browser

+0

「反應非常快,因爲它不會直接與DOM對話。」單靠第一行就能幫助我理解發生了什麼。感謝這個答案!我會測試一下,看看它是否會有所幫助。 –

相關問題