简体   繁体   English

html-react-parser 不会在 select 标签上解析 onChange function

[英]html-react-parser will not parse onChange function on select tag

I am attempting to dynamically create multiple select drop downs in a loop.我正在尝试在循环中动态创建多个 select 下拉菜单。 The html-react-parser works fine when there is just pure html in it.当其中只有纯 html 时,html-react-parser 工作正常。 However, when I add a onChange function to the select tag, it does not parse that part correctly.但是,当我将 onChange function 添加到 select 标记时,它无法正确解析该部分。 It actually seems to just disregard it.它实际上似乎只是无视它。

 let selectOptions = "<th>Details</th>";
 for(var j=0;j<envsToCommitsHolder.length;j++){
        selectOptions += "<th><select onChange={(event) => {handleOptionSelection(event.target.selectedOptions)}} id="+realRepoName[j+1]+"><option>"+realRepoName[j+1]+"</option>";       
        for(var h=0;h<envsToCommitsHolder[j].val.length;h++){
          console.log(realRepoName[j+1]);
          console.log(envsToCommitsHolder[j].val[h]);    
          //var value = envsToCommitsHolder[j].val[h];      
          selectOptions += "<option value="+h+">"+envsToCommitsHolder[j].val[h]+"</option>";          
        }
        selectOptions += "</select></th>";      
      }     
      return parse(selectOptions);     

Please see the attached image to see how it is being rendered.请查看所附图片以查看其渲染方式。 It puts the javascript outside of the select tag altogether.它将 javascript 完全放在 select 标签之外。 Is there some other way I should be doing this?还有其他方法我应该这样做吗? I have attached the image of how the select with the onChange is being rendered on the browser -> problematic part .我附上了带有 onChange 的 select 如何在浏览器上呈现的图像 ->有问题的部分 I also saw there is a "dangerouslySetInnerHTML" to parse string and render html out of it, but I am not sure how to use that or if it will be useful in this case or not.我还看到有一个“dangerouslySetInnerHTML”来解析字符串并从中呈现 html,但我不确定如何使用它或者它在这种情况下是否有用。 Any help would be greatly appreciated.任何帮助将不胜感激。 I am quite stuck with this one.我非常坚持这个。

I have created a sandbox to render the select options dynamically, you can custom the rendering on your own, https://codesandbox.io/s/react-typescript-forked-8psy13?file=/src/App.tsx我创建了一个沙箱来动态渲染 select 选项,你可以自定义渲染, https://codesandbox.io/s/react-typescript-forked-8psy13?file=/src/App.tsx
Basic React knowledge: useState, useEffect If y have a question, you can ask me here React基础知识:useState, useEffect 有问题可以在这里问我

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

相关问题 HTML-React-Parser 返回 object - 不是字符串? - HTML-React-Parser returning object - not string? 使用 react html-react-parser 或 innerHTML 解析 html 字符串? 哪个更快? - parse html string using react html-react-parser or innerHTML? which is faster? 反应onChange功能不适用于选择标签 - react onChange function does not work for select tag 在 React SharePoint WebPart 中,使用“html-react-parser”和使用“dompurify eslint-plugin-risxss”来安全地显示 HTML 之间有什么区别 - In React SharePoint WebPart what are the differences between using 'html-react-parser' & using 'dompurify eslint-plugin-risxss' to securely show HTML 使用js制作html按钮更新选择标签onchange函数 - use js to make html button update select tag onchange function Javascript onchange函数无法处理select标记 - Javascript onchange function not working on select tag 在javascript中选择标签而不调用onchange hander函数 - Select tag in javascript not calling onchange hander function 如何为JavaScript使用onchange函数选择标记 - How to use onchange function select tag for javascript 选择标记中的onchange不调用函数 - onchange in select tag does not call function HTML中的“未捕获的TypeError:bgColor不是函数”选择onchange - “Uncaught TypeError: bgColor is not a function” in HTML select onchange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM