jQuery 替换功能没有替换有效 HTML 中的文本?

我正在尝试替换 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: &quot;100%&quot;, placeholder: &quot;Select a type...&quot;}" tabindex="-1" aria-hidden="true">


呼如林
浏览 98回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript