简体   繁体   English

foreach循环仅选中一个复选框

[英]foreach loop only select one checkbox

I currently have a foreach loop populating a table with JSON results and with some help on another question i was able to get the table to work correctly with a checkbox however when i select one checkbox all of the checkboxes get selected. 我目前有一个foreach循环,其中用JSON结果填充了一个表,并且在另一个问题的帮助下,我能够使该表与一个复选框一起正常工作,但是当我选中一个复选框时,所有复选框均被选中。 how would i go about differentiating them so they can all be selected individually. 我将如何区分它们,以便可以单独选择它们。

    <tbody data-bind="foreach: policies">
        <tr>
            <td><input type="checkbox" data-bind="checked: $parent.queued" /></td>
            <td data-bind="text: policy_number"></td>
            <td data-bind="text: policy_type"></td>
            <td data-bind="text: contact.first"></td>
            <td data-bind="text: contact.last"></td>
            <td data-bind="text: contact.street"></td>
            <td data-bind="text: contact.city"></td>
            <td data-bind="text: contact.state"></td>
            <td data-bind="text: contact.zipcode"></td>
            <td data-bind="text: contact.phonenumber"></td>
        </tr>
    </tbody>
</table>
<a data-bind="visible: queued" class="btn btn-lg btn-primary btn-block" data-bind="click: generate">Generate</a>
 <!--class="clickable" data-bind="click: generate"-->
<script type="text/javascript">
/* global ko, $ */
function Policy(data) {
    var self = this;
    Object.keys(data).forEach(function(prop) {
        self[prop] = data[prop];
    });

    self.generate = function() {
        window.open("{{ url_for('genreport') }}/" + qvm.letter() + '/' + self.id);
    }

}


function QueryViewModel(){
    var self = this;

    self.first = ko.observable('');
    self.last = ko.observable('');
    self.phone = ko.observable('');

    self.letter = ko.observable();

    self.letters = {{ letters|safe }};

    self.policies = ko.observableArray();
    self.queued = ko.observableArray(false);

    self.clear = function() {
        self.policies.removeAll();
        self.first('');
        self.last('');
        self.phone('');
    }

    self.search = function() {
        // postJson here
        var queryObj = {
            first: self.first(),
            last: self.last(),
            phone: self.phone()
        }

        $.postJSON("{{ url_for('report_search') }}", queryObj, function(result) {
            // first empty our policy table
            self.policies([]);

            // add results
            result.policies.forEach(function(p) {
                self.policies.push(new Policy(p));
            });
        });
    }    

}

var qvm = new QueryViewModel()

ko.applyBindings(qvm);
</script>

You could try using an array and using checked value 您可以尝试使用数组并使用检查值

<td><input type="checkbox" data-bind="checked: $data.queuedValues, checkedValue: policyNumber" /></td>

function QueryViewModel(){

    var self = this;
....
self.queuedValues=ko.observableArray([]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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