我有一个Knockout.js Web应用程序,其中有一个select2下拉菜单。我想将id和text值都绑定到变量上,而不仅仅是id。这是我的数据:
var cars = [{id: 1, name: 'Honda'}, {id: 2, name: 'Toyota'}, {id: 3, name: 'Dodge'}];
var selectedCar = ko.observable();
这是我的html:
<select data-bind="value: selectedCar, optionsCaption: 'Select', optionsText: 'name', options: cars"></select>
现在,每当我在下拉列表中选择某项时,我的变量就包含整个对象,如下所示:
selectedCar = {id: 1, name: 'Honda'};
唯一的问题是在您加载页面并希望将下拉菜单设置为特定值时发生。即使在呈现html之前将selectedCar
变量设置为{id: 1, name: 'Honda'}
(当页面呈现下拉菜单时)并未设置为任何值,它只是设置为占位符“选择”。
我在做什么错了?
答案 0 :(得分:1)
如果要将id和text值都绑定到变量,则需要使用optionsValue
绑定并将整个上下文绑定到该变量($data
);
<select data-bind="value: selectedCar,
optionsCaption: 'Select',
optionsText: 'name',
options: cars,
optionsValue: $data"></select>
通常,我们将指定ID或其他以获得初始值的内容。因此,在您的问题中,如果将湿对象集{id: 1, name: 'Honda'}
而不是selectedCar
设置为optionsValue: $data
,则将整个对象$optionsValue: 'id'
作为初始值提供给cars
就很有意义。
(重要)但事实证明这是行不通的,因为我们正在创建一个新对象,因此当Knockout的相等性测试将selectedCar
的对象与cars[0]
中的对象。设置初始值的正确方法是this
。
我确定这是一个错字,但是无论如何我都会指定:创建任何需要HTML访问的变量时,您需要将其绑定到this.cars = [{id: 1, name: 'Honda'}, {id: 2, name: 'Toyota'}, {id: 3, name: 'Dodge'}];
this.selectedCar = ko.observable();
,这将是对viewModel。
var viewModel = function(){
var self = this;
self.cars = [{id: 1, name: 'Honda'}, {id: 2, name: 'Toyota'}, {id: 3, name: 'Dodge'}];
self.selectedCar = ko.observable(self.cars[0]);
};
ko.applyBindings(new viewModel());
让我们用小提琴测试所有这些:
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<select data-bind="value: selectedCar, optionsCaption: 'Select', optionsText: 'name', optionsValue:$data, options: cars"></select>
<!-- to verify that we are getting the entire object -->
<p data-bind="text: ko.toJSON(selectedCar)"></p>
1: Download of product 'mysql-server' started from http://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.12-winx64.msi
1: Download of package 'MySQL Server 8.0.12' succeeded
1: Action 23:38:55: INSTALL.
1: 1: MySQL Server 8.0 2: {29AEA9CC-3A6A-4F86-8A0E-AC8750F91B69}
1: Action 23:38:55: FindRelatedProducts. Searching for related applications
1: Action 23:38:55: AppSearch. Searching for installed applications
1: Action 23:38:55: LaunchConditions. Evaluating launch conditions
1: Action 23:38:55: ValidateProductID.
1: Action 23:38:55: CostInitialize. Computing space requirements
1: Action 23:38:55: FileCost. Computing space requirements
1: Action 23:38:55: CostFinalize. Computing space requirements
1: Action 23:38:55: MigrateFeatureStates. Migrating feature states from related applications
1: Action 23:38:55: InstallValidate. Validating install
1: Action 23:38:56: SaveTargetDir.
1: Action 23:38:56: InstallInitialize.
1: Action 23:38:56: RemoveExistingProducts. Removing applications
1: Action 23:38:56: ProcessComponents. Updating component registration
1: Action 23:38:56: GenerateScript. Generating script operations for action:
1: Updating component registration
1: Action 23:38:56: UnpublishFeatures. Unpublishing Product Features
1: Action 23:38:56: SchedSecureObjectsRollback.
1: Action 23:38:56: RemoveRegistryValues. Removing system registry values
1: Action 23:38:56: RemoveShortcuts. Removing shortcuts
1: Action 23:38:56: RemoveFiles. Removing files
1: Action 23:38:56: RemoveFolders. Removing folders
1: Action 23:38:56: CreateFolders. Creating folders
1: Folder: Creating folders
1: Action 23:38:56: InstallFiles. Copying new files
1: File: Copying new files, Directory: , Size:
1: Action 23:38:56: CreateShortcuts. Creating shortcuts
1: Shortcut: Creating shortcuts
1: Action 23:38:56: WriteRegistryValues. Writing system registry values
1: Key: Writing system registry values, Name: , Value:
1: Action 23:38:56: InstallServices. Installing new services
1: Action 23:38:56: SchedSecureObjects.
1: Action 23:38:56: ExecSecureObjects.
1:
1: Action 23:38:56: RegisterUser. Registering user
1: Action 23:38:56: RegisterProduct. Registering product
1: Registering product
1: Action 23:38:56: PublishFeatures. Publishing Product Features
1: Feature: Publishing Product Features
1: Action 23:38:56: PublishProduct. Publishing product information
1: Action 23:38:56: InstallFinalize.
1: Action 23:38:56: ProcessComponents. Updating component registration
1: Action 23:38:57: CreateFolders. Creating folders
1: Folder: C:\ProgramData\MySQL\MySQL Server 8.0\
1: Folder: C:\ProgramData\MySQL\MySQL Server 8.0\data\
1: Folder: C:\ProgramData\MySQL\MySQL Server 8.0\data\mysql\
1: Folder: C:\ProgramData\MySQL\MySQL Server 8.0\data\performance_schema\
1: Folder: C:\ProgramData\MySQL\MySQL Server 8.0\data\test\
1: Action 23:38:57: InstallFiles. Copying new files
1: File: ibd2sdi.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5097472
1: File: innochecksum.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5061120
1: File: libeay32.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 2102272
1: File: libmecab.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 1839616
1: File: lz4_decompress.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4946432
1: File: my_print_defaults.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4955136
1: File: myisam_ftdump.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5217792
1: File: myisamchk.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5327872
1: File: myisamlog.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5177856
1: File: myisampack.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5239808
1: File: mysql.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5290496
1: File: mysql_config.pl, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 7392
1: File: mysql_config_editor.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4972544
1: File: mysql_secure_installation.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5178880
1: File: mysql_ssl_rsa_setup.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5004288
1: File: mysql_tzinfo_to_sql.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4900864
1: File: mysql_upgrade.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 11030016
1: File: mysqladmin.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5186560
1: File: mysqlbinlog.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5476352
1: File: mysqlcheck.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5198336
1: File: mysqld.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 45210624
1: File: mysqld_multi.pl, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 28045
1: File: mysqldump.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5259264
1: File: mysqldumpslow.pl, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 8071
1: File: mysqlimport.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5179904
1: File: mysqlpump.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5664256
1: File: mysqlshow.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5178368
1: File: mysqlslap.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5196288
1: File: perror.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 5593600
1: File: resolveip.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4953600
1: File: ssleay32.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 356864
1: File: zlib_decompress.exe, Directory: C:\Program Files\MySQL\MySQL Server 8.0\bin\, Size: 4920832
1: File: binary_log_types.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 2886
1: File: errmsg.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 5294
1: File: my_command.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 4385
1: File: my_list.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 2603
1: File: mysql.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 31305
1: File: client_plugin.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\mysql\, Size: 7295
1: File: mysql_com.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 34068
1: File: plugin_auth_common.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\mysql\, Size: 5546
1: File: mysql_time.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 2996
1: File: udf_registration_types.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\mysql\, Size: 3848
1: File: mysql_version.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 1054
1: File: mysqld_error.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 193371
1: File: mysqlx_ername.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 4745
1: File: mysqlx_error.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 2954
1: File: mysqlx_version.h, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\, Size: 1861
1: File: applink.c, Directory: C:\Program Files\MySQL\MySQL Server 8.0\include\openssl\, Size: 3176
1: File: libmysql.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\, Size: 5164032
1: File: libmysql.lib, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\, Size: 27066
1: File: char.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 262496
1: File: dicrc, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 693
1: File: left-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 55910
1: File: matrix.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 3463716
1: File: pos-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 1477
1: File: rewrite.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 6241
1: File: right-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 55910
1: File: sys.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 37779919
1: File: unk.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_euc-jp\, Size: 5409
1: File: char.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 262496
1: File: dicrc, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 693
1: File: left-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 55910
1: File: matrix.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 3463716
1: File: pos-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 1477
1: File: rewrite.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 6241
1: File: right-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 55910
1: File: sys.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 37635899
1: File: unk.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_sjis\, Size: 5409
1: File: char.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 262496
1: File: dicrc, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 693
1: File: left-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 55910
1: File: matrix.bin, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 3463716
1: File: pos-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 1477
1: File: rewrite.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 6241
1: File: right-id.def, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 55910
1: File: sys.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 49199027
1: File: unk.dic, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\dic\ipadic_utf-8\, Size: 5690
1: File: mecabrc, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\mecab\etc\, Size: 1403
1: File: mysqlclient.lib, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\, Size: 61569518
1: File: adt_null.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 24064
1: File: component_log_filter_dragnet.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 31744
1: File: component_log_sink_json.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 16384
1: File: component_log_sink_syseventlog.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 15360
1: File: component_log_sink_test.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 30720
1: File: component_validate_password.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 65536
1: File: connection_control.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 47104
1: File: group_replication.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 1070592
1: File: ha_example.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 28672
1: File: keyring_file.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 108544
1: File: keyring_udf.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 25088
1: File: libpluginmecab.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 30720
1: File: libtest_framework.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 11776
1: File: libtest_services.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 19456
1: File: libtest_services_threaded.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 20480
1: File: libtest_session_attach.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 49152
1: File: libtest_session_detach.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 32256
1: File: libtest_session_in_thd.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 22528
1: File: libtest_session_info.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 49664
1: File: libtest_sql_2_sessions.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 34304
1: File: libtest_sql_all_col_types.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 37888
1: File: libtest_sql_cmds_1.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 37888
1: File: libtest_sql_commit.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 39424
1: File: libtest_sql_complex.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 40960
1: File: libtest_sql_errors.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 36352
1: File: libtest_sql_lock.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 45056
1: File: libtest_sql_processlist.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 34816
1: File: libtest_sql_replication.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 37376
1: File: libtest_sql_shutdown.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 29184
1: File: libtest_sql_sqlmode.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 39424
1: File: libtest_sql_stmt.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 65024
1: File: libtest_sql_stored_procedures_functions.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 38912
1: File: libtest_sql_views_triggers.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 38400
1: File: libtest_x_sessions_deinit.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 22016
1: File: libtest_x_sessions_init.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 27136
1: File: locking_service.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 13312
1: File: mypluglib.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 13312
1: File: mysql_no_login.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 12288
1: File: rewrite_example.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 13312
1: File: rewriter.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 65536
1: File: semisync_master.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 75264
1: File: semisync_slave.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 29696
1: File: test_security_context.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 12800
1: File: test_services_plugin_registry.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 23040
1: File: test_udf_services.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 14848
1: File: validate_password.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 53760
1: File: version_token.dll, Directory: C:\Program Files\MySQL\MySQL Server 8.0\lib\plugin\, Size: 62976
1: File: LICENSE, Directory: C:\Program Files\MySQL\MySQL Server 8.0\, Size: 301518
1: File: README, Directory: C:\Program Files\MySQL\MySQL Server 8.0\, Size: 687
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\bulgarian\, Size: 258842
1: File: armscii8.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5939
1: File: ascii.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5925
1: File: cp1250.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 8654
1: File: cp1251.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 8783
1: File: cp1256.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5982
1: File: cp1257.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 9315
1: File: cp850.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5919
1: File: cp852.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5941
1: File: cp866.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 6026
1: File: dec8.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 6942
1: File: geostd8.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5929
1: File: greek.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 6141
1: File: hebrew.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5930
1: File: hp8.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5915
1: File: Index.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 19495
1: File: keybcs2.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5942
1: File: koi8r.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5923
1: File: koi8u.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 6945
1: File: latin1.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 10229
1: File: latin2.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 7651
1: File: latin5.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 5928
1: File: latin7.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 7851
1: File: macce.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 8460
1: File: macroman.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 8471
1: File: README, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 1749
1: File: swe7.xml, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\, Size: 6943
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\czech\, Size: 259348
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\danish\, Size: 259348
1: File: dictionary.txt, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 25575
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\dutch\, Size: 260368
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\english\, Size: 258589
1: File: errmsg-utf8.txt, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 826306
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\estonian\, Size: 258853
1: File: fill_help_tables.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 1023732
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\french\, Size: 259996
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\german\, Size: 266283
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\greek\, Size: 265380
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\hungarian\, Size: 259230
1: File: innodb_memcached_config.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 3999
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\italian\, Size: 260359
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\japanese\, Size: 266035
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\korean\, Size: 261629
1: File: mysql_sys_schema.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 289722
1: File: mysql_system_tables.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 109850
1: File: mysql_system_tables_data.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 5510155
1: File: mysql_system_users.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 2289
1: File: mysql_test_data_timezone.sql, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\, Size: 10830
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\norwegian\, Size: 258659
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\norwegian-ny\, Size: 258712
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\polish\, Size: 259518
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\portuguese\, Size: 261549
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\romanian\, Size: 260301
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\russian\, Size: 273927
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\serbian\, Size: 260970
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\slovak\, Size: 259115
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\spanish\, Size: 260243
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\swedish\, Size: 259442
1: File: errmsg.sys, Directory: C:\Program Files\MySQL\MySQL Server 8.0\share\ukrainian\, Size: 268501
1: Action 23:38:59: CreateShortcuts. Creating shortcuts
1: Shortcut: icon.ico
1: Shortcut: Icon.MysqlCmdShell
1: Shortcut: rpxm3ds_|MySQL 8.0 Command Line Client
1: Shortcut: 7ejegqmo|MySQL 8.0 Command Line Client - Unicode
1: Action 23:38:59: WriteRegistryValues. Writing system registry values
1: Key: \SOFTWARE\MySQL AB\MySQL Server 8.0, Name: Location, Value: C:\Program Files\MySQL\MySQL Server 8.0\
1: Key: \SOFTWARE\MySQL AB\MySQL Server 8.0, Name: Version, Value: 8.0.12
1: Key: \SOFTWARE\MySQL AB\MySQL Server 8.0, Name: DataLocation, Value: C:\ProgramData\MySQL\MySQL Server 8.0\
1: Key: \SOFTWARE\MySQL AB\MySQL Server 8.0, Name: , Value:
1: Key: \Console\MySQL 8.0 Command Line Client - Unicode, Name: FaceName, Value: Lucida Console
1: Key: \Console\MySQL 8.0 Command Line Client - Unicode, Name: FontWeight, Value: #400
1: Key: \Console\MySQL 8.0 Command Line Client - Unicode, Name: , Value:
1: Key: \Software\MySQL AB\MySQL Server 8.0, Name: installed, Value: #1
1: Action 23:38:59: ExecSecureObjects.
1: Action 23:38:59: Rollback. Rolling back action:
1: ExecSecureObjects
1: Writing system registry values
1: Creating shortcuts
1: Copying new files
1: Creating folders
1: Updating component registration
1: 1: MySQL Server 8.0 2: {29AEA9CC-3A6A-4F86-8A0E-AC8750F91B69} 3: 3
1: The action 'Install' for product 'MySQL Server 8.0.12' failed.
答案 1 :(得分:0)
选择框的value
将成为与所选项目或optionsValue
参数中设置的属性相对应的对象。因此,对于对象,您设置的所选值必须与数组中存在的实例相同。具有对象的不同实例恰好在结构上等效是不够的。
在这种情况下,我发现将选择框的值绑定到对象的唯一ID更容易。然后,您可以通过计算值将该ID映射到所需的实际实例。
function ViewModel(data) {
this.cars = data.cars;
this.selectedCarId = ko.observable(data.selectedCarId);
this.selectedCar = ko.computed(() => {
let selectedCarId = this.selectedCarId();
return this.cars.find(c => c.id === selectedCarId);
});
}
let model = {
cars: [
{ id: 1, name: 'Honda' },
{ id: 2, name: 'Toyota' },
{ id: 3, name: 'Dodge' }
],
selectedCarId: 2
};
ko.applyBindings(new ViewModel(model), document.getElementById('content'));
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div id="content">
<select data-bind="value: selectedCarId,
optionsCaption: 'Select',
optionsText: 'name',
optionsValue: 'id',
options: cars">
</select>
<p>selectedCarId: <span data-bind="text: selectedCarId"></span></p>
<p>selectedCar: <span data-bind="text: ko.toJSON(selectedCar)"></span></p>
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
</div>
如果您不想使用单独的计算属性,则仍然需要传递索引,但是必须从数组中获取设置的值。
function ViewModel(data) {
this.cars = data.cars;
this.selectedCar = ko.observable(
data.cars.find(c => c.id === data.selectedCarId)
);
}