Knockout选项绑定获得价值

时间:2013-11-21 15:53:09

标签: knockout.js

我很难获得选项绑定的选定值。

这是(简化)代码:

HTML

  <div data-bind="foreach:organizations" class="accordion " id="accordionOrg">
       [...]
    <table>
       <thead>
         <tr>
             <th>Billing Address</th>
             [...]
             <th></th>
         </tr>
       </thead>
       <tbody>
          <tr> 
             <td>
               <select data-bind="options: organizationAddresses, optionsText: 'address', value:'organizationAddressId'"></select> 
             </td>
             <td>
                <div data-bind="click: $root.addOrg" class="btn btn-small  btn-block"></div>
             </td>
          </tr>
       </tbody>
    </table>
  </div>

每个组织都有ICollection地址: C#模型

public class Organization
{
    public int OrganizationId { get; set; }
    public string Name { get; set; }
    public string SapId { get; set; }
    public string UserId { get; set; }
    public bool Deleted { get; set; }

    public ICollection<OrganizationAddress> OrganizationAddresses { get; set; }

}

视图模型:

var organizations = ko.observableArray(); 
var vm = {
    organizations: organizations,
    activate: activate        
};

return vm;

function addOrg(organization) {
   //here I need the selected value
}

一切正常我只是不知道如何获得所选值。 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

啊哈哈我想得太远......我完全忘记了Jquery。随着

  

optionsValue

绑定我可以通过以下方式获取值:

 $('#' + organization.organizationId()).val();

Thx为您的输入Origineil! 有时你必须写下问题才能找到答案:)