最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - knockout binding on dynamically generated elements - Stack Overflow

programmeradmin1浏览0评论

I am using Knockout.js and i am pretty new in this. I created a Example to my problem. Here i am trying to bind the knockout binding to dynamically generated elements. But the binding are not applied properly to dynamically generated elements.

I am trying to synchronize the input text field with label element. So whatever we enter in the input field the same text will reflect in its corresponding label element. Please forgive me if i am not clear with my question and please ask me for clearance. Help me guys ? thanks.

I am using Knockout.js and i am pretty new in this. I created a Example to my problem. Here i am trying to bind the knockout binding to dynamically generated elements. But the binding are not applied properly to dynamically generated elements.

I am trying to synchronize the input text field with label element. So whatever we enter in the input field the same text will reflect in its corresponding label element. Please forgive me if i am not clear with my question and please ask me for clearance. Help me guys ? thanks.

Share Improve this question edited Oct 18, 2012 at 11:00 Tom Rider asked Oct 18, 2012 at 10:45 Tom RiderTom Rider 2,8157 gold badges44 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In your code you are not using one of the main feature of knockout - auto-generating html. Instead of using jQuery to add new row - use knockout foreach binding with observableArray. When you add new item to array knockout will automatically generate html markup.

Javascript:

function CourseViewModel(){
    var self = this;
    self.textValue = ko.observable('');
}

function CeremonyViewModel() {
    var self = this;

    self.cources = ko.observableArray([new CourseViewModel()]);

    self.addCourse = function(){
        self.cources.push(new CourseViewModel());
    };
}

ko.applyBindings(new CeremonyViewModel());

Html:

 <div id="menutab">
    <form id="createMForm">
        <input type="button" id="createmenu" value="Add menu" data-bind="click: addCourse"/>
        <div class="menu">
            <table data-bind="foreach: cources" class="ui-widget ui-widget-content" >
                <tr>
                    <td>
                        <label for="CourseName">CourseName</label>
                    </td>
                    <td>
                        <input type="text"  data-bind="value: textValue, valueUpdate:'keyup'" class="CreateCourseName" name="CourseName" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</div>
<div id="courseoptiontab">
    <form id="createCOForm">
        <div class="options">
            <table data-bind="foreach: cources" class="ui-widget ui-widget-content">
                <tr>
                    <td>
                        <label class="colabel">Course Name
                            <span class="forcourse" data-bind="text: textValue"></span>
                        </label>
                    </td>
                </tr>
          </table>
       </div>
    </form>
<div>

Here is working fiddle: http://jsfiddle/vyshniakov/g5vxr/

发布评论

评论列表(0)

  1. 暂无评论