javascript - 如何让 jQuery 选择带有 . (句号)在他们的身份证?

标签 javascript jquery jquery-selectors

给定以下类和 Controller 操作方法:

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 实例和学校的 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);
    });
  });
});

最佳答案

来自 Google Groups :

Use two backslashes before each special character.

A backslash in a jQuery selector escapes the next character. But you need two of them because backslash is also the escape character for JavaScript strings. The first backslash escapes the second one, giving you one actual backslash in your string - which then escapes the next character for 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);
    });
  });
});

还可以查看 How do I select an element by an ID that has characters used in CSS notation?在 jQuery 常见问题解答中。

关于javascript - 如何让 jQuery 选择带有 . (句号)在他们的身份证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/350292/

相关文章:

javascript - 用于 div 的多个子级别输入元素的 JQuery 选择器

javascript - CSS 关键帧动画的随机 "start point"

javascript - 如何加速 JavaScript 图片加载

javascript - 使用 jQuery 为点击时的图像大小设置动画

javascript - 添加事件处理程序以循环动态创建的播放列表(YouTube API)

jquery - 从组合框的值获取选项的文本

javascript - jQuery 选择器、查找和获取之间的区别

javascript - 多个模态窗口尝试在 JS 中使用正则表达式

javascript - 映射对象以将对象转换为数组

jquery - 如何将前两个图像放在一列和其余两列上?