如何让jQuery选择带有的元素。 (期间)他们的身份证? [英] How do I get jQuery to select elements with a . (period) in their ID?

查看:89
本文介绍了如何让jQuery选择带有的元素。 (期间)他们的身份证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下类和控制器操作方法:

Given the following classes and controller action method:

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>

我可以更新学校实例和学校的地址成员。这太好了!谢谢ASP.NET MVC团队!

I am able to update both the School instance and the Address member of the school. This is quite nice! Thank you ASP.NET MVC team!

但是,如何使用jQuery选择下拉列表以便我可以预先填充它?我意识到我可以做这个服务器端,但页面上会有其他影响列表的动态元素。

However, how do I use jQuery to select the drop down list so that I can pre-fill it? I realize that I could do this server side but there will be other dynamic elements on the page that affect the list.

以下是我到目前为止所做的以及它因为选择器似乎与ID不匹配,所以不起作用:

The following is what I have so far, and it does not work as the selectors don't seem to match the IDs:

$(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网上论坛


在每个特殊字符前使用两个反斜杠。

Use two backslashes before each special character.

jQuery选择器中的反斜杠会转义下一个字符。但是你需要
其中两个因为反斜杠也是JavaScript
字符串的转义字符。第一个反斜杠转义第二个,在你的字符串中给你一个实际的
反斜杠 - 然后转义为jQuery的下一个字符。

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);
    });
  });
});

同时查看如何选择元素在jQuery常见问题解答中使用CSS表示法中使用的字符?

这篇关于如何让jQuery选择带有的元素。 (期间)他们的身份证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆