How do I get jQuery to select elements with a . (period) in their ID?

后端 未结 8 1234
南笙
南笙 2020-11-22 13:16

Given the following classes and controller action method:

public School
{
  public Int32 ID { get; set; }
  publig String Name { get; set; }
  public Address         


        
8条回答
  •  情深已故
    2020-11-22 13:40

    From 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.

    So, I guess you're looking at

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

    Also check out How do I select an element by an ID that has characters used in CSS notation? on the jQuery FAQ.

提交回复
热议问题