简体   繁体   中英

KnockoutJS: Posting only visible form elements

I have just started using Knockout.js My form has elements which I call questions. I hide/show them based on user selections. When user hits the submit button I want to post only the visible questions at the time of submit. What I have is this:

 // length of Results(questionArray) is 260
            var vmToPost = viewModel;
            delete vmToPost.__ko_mapping__;
            ko.utils.arrayForEach(vmToPost.Results(), function (question) {
                if (!(vmToPost.getQuestion(question.QuestionID()).visible())) {
                    ko.utils.arrayRemoveItem(vmToPost.Results(), question);
                }
            });

The util function arrayForEach is behaving strange. It loops through the array very differntly. I had to hit the submit button 7 times to get all the visible elements and come out of the util function. It doesnt throw any error message in the console or the fiddler. What am I doing wrong. Please help.

Html contains a built-in way to skip items from being submitted. It's the disabled attribute , which can be controlled using Knockout with the enable or disable bindings.

<div data-bind="visible: visible">
    <label>Name: <input name="name" data-bind="enable: visible"></label>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM