Unable to clear input field when Add input data using Knockout,Input fields clear using jquery but when click on Add button again then values still exist and added.

85 views
Skip to first unread message

imran Ullah khan

unread,
Oct 20, 2016, 10:33:20 AM10/20/16
to KnockoutJS
var RequestModel = @Html.Raw(Json.Encode(Model))
$(document).ready(function () {
            CreateAndBindModel();
            ko.applyBindings(RequestModel);
});

        function CreateAndBindModel() {
        RequestModel.SelectedMaterialOrder.Name =ko.observable();

            RequestModel.lstMaterialOrder = ko.observableArray();
            RequestModel.AddValue = function ()
            {
                    var objModel = ko.toJS(RequestModel.SelectedMaterialOrder);
                    RequestModel.lstMaterialOrder.push(objModel);

                RequestModel.SelectedMaterialOrder.Name=null;
            }
            RequestModel.SelectedMaterialOrder = null;
            RequestModel.removeItem = function (data) 
      {
           //debugger
             RequestModel.lstMaterialOrder.remove(data);}
        }

}

<td><input type="text" maxlength="100" id="Name" style="width: 200px" data-bind="value:RequestModel.SelectedMaterialOrder.Name"></td>
<tr><td><button type="button" data-bind="click: AddValue">Add</button></td></tr>
//Display Input value here
                        <!-- ko foreach:RequestModel.lstMaterialOrder -->
                        <tr>
                            <td  style="padding-left:5px;" data-bind="text: Name"></td>
                            <td><a href="#" data-bind="click: $parent.removeItem">Remove</a></td>
                        </tr>
                        <!--/ko-->


Michael Best

unread,
Oct 20, 2016, 1:04:05 PM10/20/16
to KnockoutJS
To set an observable value, always use it as a function:


Instead of 

imran Ullah khan

unread,
Oct 24, 2016, 7:36:24 AM10/24/16
to knock...@googlegroups.com
Thanks God bless you

Imran Ullah Khan
.Net Developer
 iENGINEERING (Pvt) Islamabad Pakistan
00923329737036

--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/imkSpOPRubs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages