我试图在onclick
函数中传递一个参数,但它在ReactJS中显示未定义。
我的代码(or here):
var Tablelist = React.createClass({
createNewoder: function(iid) {
var tbn = this.iid;
$.ajax({
type: 'POST',
headers: {
// Accept : "application/json",
"Content-Type": "application/json"
},
success: function(data) {
console.log(data);
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
// console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function(){
return {
tablelist : []
};
},
componentDidMount: function(){
$.ajax({
type: 'GET',
url: "/restaurant/table/list/10/1",
headers: {
Accept : "application/json",
"Content-Type": "application/json"
},
success: function(tablelist) {
this.setState({tablelist: tablelist}, function(){
console.log(this.state.tablelist);
}.bind(this));
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
render: function(){
var tablelist11 = [];
var idd ;
for(var tablenumber in this.state.tablelist){
tablelist11.push(
<div className="card">
<div className="title">
Table {this.state.tablelist[tablenumber].tablenumber}
</div>
<p> Orders: <strong>{this.state.tablelist[tablenumber].count_orders} </strong></p>
<p> Location:{this.state.tablelist[tablenumber].location} </p>
<p> <a onClick={ this.createNewoder.bind(this, this.state.tablelist[tablenumber].tablenumber) }>Add</a></p>
</div>
);
return (
<div>
{tablelist11}
</div>
);
}
});
React.render(
<Tablelist source="/restaurant/table/list/10/1" />,
document.getElementById('list_tables')
);
在第61行,我正在打印此{this.state.tablelist[tablenumber].tablenumber}
,它是一个数值并且工作正常,但现在我想在{no}中的onclick
函数中传递此值。 66,但它正在给undefined
。
我尝试了不同的选项,但没有帮助。
如何在onclick函数中将此值作为参数传递?