douti0467 2018-01-14 23:55
浏览 200
已采纳

foreach循环只选择一个复选框

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. 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>
  • 写回答

1条回答 默认 最新

  • drtsd7864 2018-01-15 01:56
    关注

    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([]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂