Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在每个表行上放置click事件,并将结果作为参数传递,以将新数据绑定到另一个源?_Javascript_Jquery_Knockout.js_Knockout 2.0 - Fatal编程技术网

Javascript 如何在每个表行上放置click事件,并将结果作为参数传递,以将新数据绑定到另一个源?

Javascript 如何在每个表行上放置click事件,并将结果作为参数传递,以将新数据绑定到另一个源?,javascript,jquery,knockout.js,knockout-2.0,Javascript,Jquery,Knockout.js,Knockout 2.0,我有以下代码,用于填充select options,并将所选值传递给视图模型中的另一个函数,该函数使用与所选值匹配的数据填充表格: 简言之,如果用户从select(选择)选项中选择ASIA(亚洲),则亚洲的所有国家/地区都绑定到该表: 如何向表中的每一行添加单击事件,以便将单击行的CountryId作为参数传递给视图模型中的另一个函数?我需要使用参数和函数来执行其他绑定。例如:国家/地区详细信息 这就是我到目前为止所做的: <div id="country-select">

我有以下代码,用于填充
select options
,并将所选值传递给视图模型中的另一个函数,该函数使用与所选值匹配的数据填充表格:

简言之,如果用户从select(选择)选项中选择ASIA(亚洲),则亚洲的所有国家/地区都绑定到该表:

如何向表中的每一行添加单击事件,以便将单击行的
CountryId
作为参数传递给
视图模型中的另一个函数?我需要使用参数和函数来执行其他绑定。例如:
国家/地区详细信息

这就是我到目前为止所做的:

<div id="country-select">
    <select data-bind="options: UniqueContinent,
                       value: SelectedContinent"></select>
</div>

<table id="country-list">
    <tr>
        <th>CountryID</th>
        <th>Country Name</th>
        <th>City</th>
        <th>Continent</th>
        <th>CountryAbbr</th>
    </tr>
    <tbody data-bind= "foreach: FilteredEntries">
    <tr>
        <td data-bind="text: CountryId"></td>
        <td data-bind="text: Country"></td>
        <td data-bind="text: City"></td>
        <td data-bind="text: Continent"></td>
        <td data-bind="text: CountryAbbr"></td>
    </tr>
    </tbody>
</table>

只需将
单击
处理程序添加到

请参阅更新的和

function ViewModel() {
    var self = this;

    self.CountryData = ko.observableArray([
  {
    "City": "KABUL",
    "Continent": "ASIA",
    "Country": "AFGHANISTAN",
    "CountryAbbr": "AF",
    "CountryId": "102120"
  },
   {
    "City": "DHAKA",
    "Continent": "ASIA",
    "Country": "BANGLADESH",
    "CountryAbbr": "BD",
    "CountryId": "102136"
  },       
  {
    "City": "BRUSSELS",
    "Continent": "EUROPE",
    "Country": "BELGIUM",
    "CountryAbbr": "BE",
    "CountryId": "102139"
  },
  {
    "City": "MINSK",
    "Continent": "EUROPE",
    "Country": "BELARUS",
    "CountryAbbr": "BY",
    "CountryId": "102138"
  }]);

    self.SelectedContinent = ko.observable('');

    self.UniqueContinent = ko.dependentObservable(function() {
        var continent = ko.utils.arrayMap(self.CountryData(),

            function(item){

                return item.Continent
            })

        return ko.utils.arrayGetDistinctValues(continent).sort();
    });

    // Call this function when changes are made
    self.FilteredEntries = ko.computed(function() {

        return ko.utils.arrayFilter(self.CountryData(), function(item) {
            // I need to use the selected value
            return item.Continent === self.SelectedContinent();
        });
    });
}

ko.applyBindings(new ViewModel)
<tr data-bind="click: $root.CountryDetails">
self.CountryDetails = function(country)
{
    doSomethingWithCountryId(country.CountryId);
}

function doSomethingWithCountryId(countryId)
{
    // ...
}