重新绑定视图并在divout js中附加div

时间:2015-09-08 08:49:54

标签: jquery knockout.js knockout-2.0 knockout-mvc

我有以下格式的问答。问题4的第一个答案有问题参考问题5.在页面加载时我想在问题4的答案下面附加问题5。

Question references

以下是HTML代码

<div data-bind="foreach: $data.QuestionsSet">
                                    <div class="primaryCaseContainer" data-bind="attr: {id: $data.Id } , visible: $data.Available, toplevel: $data.TopLevel, css: TopLevel? 'avalable': 'notavailable'">
                                        <p class="questionHeader">
                                            <span data-bind="text: $data.DisplayValue+'.'"></span>
                                            <span data-bind="html: GetEditListGuidelineQuestion.HtmlReplace($data.HtmlText)"></span>
                                        </p>
                                        <div data-bind="attr:{id: 'colapseID'+$data.Id }" class="questionContainer">

                                            <div data-bind="foreach: $data.AnswersSet">

                                                <!--ko if:$parent.Type == 2 -->
                                                <div data-bind="attr:{class: $parent.Id +' marginTopTbl'}">
                                                    <input type="checkbox" data-bind="attr:{id: $data.Id , Qid: $parent.Id , Qref: $data.QuestionRefSetStrings , Uid: $data.Uid , rel: $data.MutuallyExclusive ? 'true' : 'false'} ,checked: $data.Selected, click: $root.answerClick">
                                                    <span data-bind="html: GetEditListGuidelineQuestion.HtmlReplace($data.HtmlText)"></span>
                                                    <span><b data-bind="text: $root.findQuestionref($data.QuestionRefSet)"></b></span>
                                                    <!--ko if:$data.InputType == "text" -->
                                                    <input type="text" data-bind="if: Selected, textInput : $data.Value, attr:{id: $data.Id , name:$parent.Id,  Qid: $parent.Id, Qref: $data.QuestionRefSetStrings , Uid: $data.Uid}">
                                                    <!-- /ko -->
                                                    @Html.Partial("_MCGSubQuestionPartial")
                                                </div>
                                                <!-- /ko -->
                                                <!--ko if:$parent.Type == 1 -->
                                                <div data-bind="attr:{class: $parent.Id +' marginTopTbl'}">
                                                    <input type="radio" data-bind="value: $data.RadioOnly, attr:{id: $data.Id , name:$parent.Id,  Qid: $parent.Id, Qref: $data.QuestionRefSetStrings , Uid: $data.Uid}, checked: $data.Selected , click: $root.answerClick , appendQrefQuestion: {Qref: $data.QuestionRefSetStrings, parentAndOwnID: $data.Id+$data.QuestionRefSetStrings}">
                                                    <span data-bind="html: GetEditListGuidelineQuestion.HtmlReplace($data.HtmlText)"></span>
                                                    <span><b data-bind="text: $root.findQuestionref($data.QuestionRefSet)"></b></span>
                                                    <!--ko if:$data.InputType == "text" -->
                                                    <input type="text" data-bind="if: Selected , textInput: $data.Value , attr:{id: $data.Id , name:$parent.Id,  Qid: $parent.Id, Qref: $data.QuestionRefSetStrings , Uid: $data.Uid}">
                                                    <!-- /ko -->
                                                    @Html.Partial("_MCGSubQuestionPartial")
                                                </div>
                                                <!-- /ko -->
                                                <div data-bind="foreach: $data.QuestionRefSet">
                                                    <div style="margin-left: 25px; margin-top: 10px" data-bind="attr:{id :'showAns' +$parent.Id+ $data.Id , class:'subQuestion showAns' +$parent.Id+ $data.Id}, if: $data.QuestionRefSetStrings != null"></div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>

                                </div>

淘汰赛代码

$(document).ready(function () {
    $("#buttonPOCBack").hide();
    $("#PlanOfCareModal").hide();
    ko.applyBindings(GuidelineQuestion, document.getElementById("GuidelineQuestionAnswers"));
    GuidelineQuestion.GetGuildlineQuestion();

    ko.bindingHandlers.appendQrefQuestion =
        {
            init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
                var value = valueAccessor();
                if (value.Qref != "") {
                    $('#' + value.Qref).appendTo('#showAns' + value.parentAndOwnID);
                }
            },
            update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
                var value = valueAccessor();
                if (value.Qref != "") {
                   $('#' + value.Qref).appendTo('#showAns' + value.parentAndOwnID);


                }
            }
        };
});

问题是绑定DIV没有形成所以附加代码不会工作。我想重新绑定元素并附加引用问题。

我尝试了很多东西,但无法找到解决方案,任何人都可以帮助我做到这一点。

0 个答案:

没有答案