在每个特殊字符之前使用两个反斜杠。
jQuery选择器中的反斜杠转义下一个字符。但是您需要其中两个,因为反斜杠也是JavaScript字符串的转义字符。第一个反斜杠转义第二个反斜杠,为您的字符串提供一个实际的反斜杠-然后转义jQuery的下一个字符。
所以,我想你在看
$(function() { $.getJSON('/Location/GetCountryList', null, function(data) { $('#Address.Country').fillSelect(data); }); $('#Address.Country').change(function() { $.getJSON('/Location/GetRegionsForCountry', { country: $(this).val() }, function(data) { $('#Address.State').fillSelect(data); }); });});
还请查看如何通过具有CSS表示法中使用的字符的ID选择元素?在jQuery FAQ上。
解决方法给定以下类和控制器操作方法:
public School{ public Int32 ID { get; set; } publig String Name { get; set; } public Address Address { get; set; }}public class Address{ public string Street1 { get; set; } public string City { get; set; } public String ZipCode { get; set; } public String State { get; set; } public String Country { get; set; }}[Authorize(Roles = 'SchoolEditor')][AcceptVerbs(HttpVerbs.Post)]public SchoolResponse Edit(Int32 id,FormCollection form){ School school = GetSchoolFromRepository(id); UpdateModel(school,form); return new SchoolResponse() { School = school };}
以及以下形式:
<form method='post'> School: <%= Html.TextBox('Name') %><br /> Street: <%= Html.TextBox('Address.Street') %><br /> City: <%= Html.TextBox('Address.City') %><br /> Zip Code: <%= Html.TextBox('Address.ZipCode') %><br /> Sate: <select id='Address.State'></select><br /> Country: <select id='Address.Country'></select><br /></form>
我可以同时更新School实例和School的Address成员。这真是太好了!谢谢ASP.NET MVC团队!
但是,如何使用jQuery选择下拉列表,以便可以预先填写呢?我意识到我可以在服务器端进行操作,但是页面上还会有其他影响列表的动态元素。
以下是我到目前为止所拥有的,并且由于选择器似乎与ID不匹配而无法正常工作:
$(function() { $.getJSON('/Location/GetCountryList',null,function(data) { $('#Address.Country').fillSelect(data); }); $('#Address.Country').change(function() { $.getJSON('/Location/GetRegionsForCountry',{ country: $(this).val() },function(data) { $('#Address.State').fillSelect(data); }); });});