Unable to bind values after inserting table rows with jquery

I have this in my html:

<table class="dataTable" id="CADataTable">
<thead>
    <tr>
        <th> Type</th>
        <th> Name</th>
        <th> Adress</th>
        <th> ID Number</th>
        <th> Contact</th>
        <th> Note</th>
    </tr>   
</thead>
<tbody>
    <tr>
        <td>
            <select name="CAType" id="CAType" data-bind="value: CAType" style="width: 12em;">               
                <option>1</option> 
                <option>2</option>  
                <option>3</option>  
                <option>4</option>      
            </select>
        </td>       
<!--         <td><input type="text" name="CAType" data-bind="value: CAType" style="width: 9em;"></td> -->
        <td><input type="text" name="CAName" data-bind="value: CAName" style="width: 15em;"></td>
        <td><input type="text" name="CAAdress" data-bind="value: CAAdress" style="width: 15em;"></td>
        <td><input type="text" name="CAIdNum" data-bind="value: CAIdNum" style="width: 6em;"></td>
        <td><input type="text" name="CAContact" data-bind="value: CAContact" style="width: 10em;"></td>
        <td><input type="text" name="CANote" data-bind="value: CANote" style="width: 15em;"></td>
    </tr>
</tbody>
</table>
<button type="button" id="export" class="button" data-bind="click: newCreditRows">Add new row</button>

and a jquery code inside of a knockout js view model, witch is executed when the button is pressed:

var clickAdd = 0;
                        newCreditRows = function(){
                            clickAdd++;
                            if(clickAdd<=9){
                                $('#CADataTable tr:last').after('<tr><td><select name="CAType' +clickAdd+ '" id="CAType' +clickAdd+ '" data-bind="value: CAType' +clickAdd+ '" style="width: 12em;"><option>Съдлъжник</option> <option>Поръчител</option>   <option>3то Лице</option>   <option>ипотекарни / заложни длъжници</option>      </select></td><td><input type="text" name="CAName' +clickAdd+ '" data-bind="value: CAName' +clickAdd+ 
                                  '" style="width: 15em;"></td><td><input type="text" name="CAAdress' +clickAdd+ '" data-bind="value: CAAdress' +clickAdd+ 
                                  ' " style="width: 15em;"></td><td><input type="text" name="CAIdNum' +clickAdd+ ' " data-bind="value: CAIdNum' +clickAdd+ 
                                  '" style="width: 6em;"></td><td><input type="text" name="CAContact' +clickAdd+ '" data-bind="value: CAContact' +clickAdd+ 
                                  ' "style="width: 10em;"></td><td><input type="text" name="CANote' +clickAdd+ '" data-bind="value: CANote' +clickAdd+ '" style="width: 15em;"></td></tr>');                            
                            }else
                                alert("Maximum number reached!");
                            };

Everything is working just fine, but what I noticed is that the new rows added by the jquery code can not bind the value to the ko.observable() variables (I have everything declared correctly in my viewmodel, I'm not posting it because the code will become huge.)

I mean that the observable CAAdress1 witch is declared like this in my code: '" data-bind="value: CAAdress' +clickAdd is not working.

I'm sure that I'm missing something really small like char escaping, but I'm still too new in jquery and knockout, so I'm not able to spot it.


You are injecting html into the dom after you called the applyBindings method. So Ko is not aware of the new elements.

Take a look at this fiddle

var CA = function() {
    this.CAName = null;
    this.CAAdress= null;
    this.CAIdNum = null;
    this.CAContact = null;
    this.CAName = null;
    this.CANote= null;
    this.CAType = null;
};

var vm = {
    newCreditRows : function () {

        this.creditRows.push(new CA());
    },
    creditRows : ko.observableArray()
};


ko.applyBindings(vm);

I hope it helps.

链接地址: http://www.djcxy.com/p/14658.html

上一篇: Medians中位数

下一篇: 无法在使用jquery插入表行后绑定值