简体   繁体   English

动态生成的按钮上的React.js Onclick事件未触发

[英]React.js Onclick event on a dynamically generated button is not firing

So I am new to React.js and have gotten stuck on writing a fucntionality. 因此,我是React.js的新手,并且被困在编写功能上。 I have a form in which i am trying to add a set of data a numerical value & a web address. 我有一种表单,试图在其中添加一组数据,数值和网址。 Once a user clicks the add button, a row is dynamically generated within the form with the values displayed and option to either edit or delete the said value. 用户单击添加按钮后,将在表单中动态生成一行,其中显示的值以及用于编辑或删除所述值的选项。 The user can add as many of these rows as they want. 用户可以根据需要添加任意数量的这些行。

The code to generate these dynamic rows is below 生成这些动态行的代码如下

 let orderStandardDetails = '<div id={standardInfo_'+orderStandardInformationChildNodesLength+'} class="standardInfo"><br /><input id="orderStandard_'+orderStandardInformationChildNodesLength+'" class="orderStandardInfo" type="text" value="ISO '+orderStandard.toString()+'" name="standardInfo" disabled/>' + '<input id={orderStandardUrl_'+orderStandardInformationChildNodesLength+'} type="text" class="orderStandardUrlInfo" value="'+orderStandardUrl+'" disabled/>' + '<button class="orderStandardEdit" id={orderStandardEdit_'+orderStandardInformationChildNodesLength+'} onClick={this.editOrderStandard}><img src="app/images/Edit.svg" /></button>' + '<button class="orderStandardDelete" id={orderStandardDelete_'+orderStandardInformationChildNodesLength+'} onClick={this.deleteOrderStandard()}><img src="app/images/Delete.svg" /></button>' + '<button class="orderStandardSave" id={orderStandardSave_'+orderStandardInformationChildNodesLength+'} onClick={this.saveOrderStandard()}><img src="app/images/Save.svg" /></button></div>'; 

Also, I am binding the function as follows inside the constructor 另外,我在构造函数中将函数绑定如下

 this.editOrderStandard = this.editOrderStandard.bind(this); this.deleteOrderStandard = this.deleteOrderStandard.bind(this); this.saveOrderStandard = this.saveOrderStandard.bind(this); 

The said function is defined as follows 所述功能定义如下

 editOrderStandard(){ alert("Edit"); } deleteOrderStandard(){ alert("Delete"); } saveOrderStandard(){ alert("Save"); } 

The issue is that none of the above three functions are firing on clicking teh button. 问题是,单击这三个按钮时,以上三个功能均未激活。 Moreover, in the firefox console i cannot see any errors pertaining to onclick. 此外,在firefox控制台中,我看不到任何与onclick有关的错误。

Please help. 请帮忙。

First things first, you should avoid to do DOM manipulations and let react take care of it. 首先,您应该避免进行DOM操作,而让react处理它。 As suggested to you in comments, you should use JSX. 如注释中建议的那样,您应该使用JSX。 For more info: JSX 有关更多信息: JSX

You can create stateless components which you want to dynamically render and let the parent component take care of state changes. 您可以创建要动态呈现的无状态组件,并让父组件负责状态更改。 Below sample is for reference. 下面的示例仅供参考。

 const OrderStandardDetails =(props) => { return ( <div class = "standardInfo"> <input class = "orderStandardInfo" type = "text" value = "ISO" name = "standardInfo" disabled / > <input type = "text" class = "orderStandardUrlInfo" value = "" disabled / > <button class = "orderStandardEdit" onClick = {props.onEdit}>Edit</button> </div> ); }; class App extends React.Component { constructor(props) { super(props); this.onEdit = this.onEdit.bind(this) } onEdit() { alert("Edit"); } render() { return <div> <OrderStandardDetails onEdit={this.onEdit} /> </div> }; } ReactDOM.render( < App / > , document.getElementById("app")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

If you're using redux, check out redux-form 's fieldArray . 如果您使用的是redux,请查看redux-formfieldArray I think the example provides functionality similar to what you are looking for. 我认为该示例提供的功能与您正在寻找的功能相似。

Redux Form - Field Arrays Example Redux表单-字段数组示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React.js 中的按钮上选中/取消选中带有 onClick 事件的复选框? - How to check/uncheck a checkbox with onClick event on a button in React.js? react.js-如何在动态生成的按钮成功添加元素? - react.js - how to add element on success in dynamically generated button? 函数在React.js中的onClick事件上不起作用 - Function not working on onClick event in React.js React.js 显示带有 onClick 事件的组件 - React.js Display a component with onClick event OnClick React.js 中的事件绑定 - OnClick Event binding in React.js onClick 在 React.js 中没有使用 SweetAlert 模式触发 - onClick not firing withing a SweetAlert modal in React.js 何时在按钮 onClick 事件上使用内联函数 - Javascript/React.js - When to use inline function on button onClick event - Javascript/React.js 由 onMouseOver 触发的 Material-UI 弹出框阻止按钮的 onClick 事件 - React.js - Material-UI popover triggered by onMouseOver is blocking onClick event of button - React.js 使用react.js JSX页面时onClick按钮事件处理程序不起作用 - onClick button event handler not working when using react.js JSX pages 嵌套按钮时如何仅激活顶级按钮的onclick事件? React.js - How to only activate the onclick event of the top level button when having nested buttons? React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM