2015-05-31 78 views
-1

我正在尝试构建一个包含基于状态的不同组件的动态div。我有我的自定义组件活动,稳定和形式。Reactjs窗体onSubmit无法正常工作

render: function() { 
    var components = []; 
    if (this.state.isActive) { 
    components.push(React.DOM.Active)); 
    } 
    if (this.state.isStable) { 
    components.push(React.DOM.Stable({})); 
    } 
    if (this.state.visible) { 
    components.push(React.DOM.Form({})); 
    } 
    return React.DOM.div({}, components); 
} 

的表单组件是一个简单的形式,其具有

handleSubmit(e) { 
    e.preventDefault(); 
    console.log('Submitted'); 
} 
render: function() { 
    return React.DOM.form({onSubmit: this.handleSubmit}, [React.DOM.button({},'Submit')]); 

}

的handleSubmit方法不会被调用。但如果我将代码更改为:

render: function() { 
    var components = []; 
    components.push(React.DOM.Form({})); 
    return React.DOM.div({}, components); 
} 

有谁知道问题可能来自哪里?

+0

当您更改代码时会发生什么?你没有指定。 – SevenBits

+0

糟糕!我忘了指定它!它在控制台中写入“已提交”,并且阻止了该事件的默认行为。 –

回答

0

您的React.DOM.form调用中需要{onSubmit: this.handleSubmit}

+0

我已经写了,但我忘了粘贴它。 –

1

handleSubmit方法写入不正确。它应该是

handleSubmit: function(e) { 
    e.preventDefault(); 
    console.log('Submitted'); 
}, 
//(dont forget the comma to separate methods :)