我正在尝试替换 html 字符串中的一些文本,但它会引发Uncaught TypeError: Cannot read property 'replace' of undefined错误。var formHtml = ($('#cx-record-answer-form-div .'+$('#cx-user-survey-questions-record-edit').find('#answer_type').val()).html());在当我登录了控制台formHtml变量它说,formData是不确定的,但是当我删除.html()从上面的代码的最后返回有效的HTML。但是替换函数仍然抛出未定义的错误!JS:var $addAnswerButton = $('#add-answer-button');var newAnswerFormIndex = 0;$addAnswerButton.on('click', function() { newAnswerFormIndex++; if($.trim($('#cx-user-survey-questions-record-edit').find('#answer_type').val()) != ''){ // get form html var formHtml = ($('#cx-record-answer-form-div .'+$('#cx-user-survey-questions-record-edit').find('#answer_type').val()).html()); console.log(formHtml); var appendFormHtml = formHtml.replace('cx-record-answer-form-<#id#>','cx-record-answer-form-new-'+newAnswerFormIndex); console.log(appendFormHtml); appendFormHtml = appendFormHtml.replace(/\<#id#>/g,newAnswerFormIndex); appendFormHtml = appendFormHtml.replace('bootstrapSwitch-class','bootstrapSwitch'); $('#answer-container').append(appendFormHtml); }});HTML 标记:<form id="cx-user-survey-questions-record-edit"> <div class="row"> <section class="col col-6"> <label class="label">Select type</label> <label class="input"> <select id="answer_type" name="answer_type" class="select2 select2-hidden-accessible" data-bind="select2Binding: answer_type, select2Options: { data: listAnswerTypes, width: "100%", placeholder: "Select a type..."}" tabindex="-1" aria-hidden="true">
添加回答
举报
0/150
提交
取消