【问题标题】:Unable to change the value of a button input field when the id contains a period character当 id 包含句点字符时,无法更改按钮输入字段的值
【发布时间】:2013-06-07 07:42:11
【问题描述】:

当按钮输入字段的 id 包含“。”时,我无法使用 jQuery 更改按钮的值。句号。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />   
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
</head>

<body>
Hello this is a JQuery test page.
<input id="my.button" type="button" value="" />

    <script type="text/Javascript">     
        $( "#my.button" ).val("Check Available");
    </script>

</body>

</html>

但是,如果我替换 '.'使用'_',它工作正常。我正在使用 jQuery 1.3.2(但我也看到了 jQuery 1.7.1 的这种行为)并且正在 Firefox 21.0 上运行这个 html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />   
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
</head>

<body>
Hello this is a JQuery test page.
<input id="my_button" type="button" value="" />

    <script type="text/Javascript">     
        $( "#my_button" ).val("Check Available");
    </script>

</body>

</html>

我在第一个代码 sn-p 中做错了什么,还是这是一个错误?

【问题讨论】:

    标签: jquery button input


    【解决方案1】:

    你需要用\\来逃避那个时期

    $( "#my\\.button" ).val("Check Available");
    

    使用任何元字符(例如 !"#$%&'()*+,./:;?@[]^{|}~ ) as a literal part of a name, it must be escaped with with two backslashes:\`。例如,一个元素 id="foo.bar",可以使用选择器$("#foo\\.bar")

    【讨论】:

    • 非常感谢!那行得通。但是,如果我有一个 span 元素,那么选择似乎可以在不转义“。”的情况下工作。 (调用 html() 函数时)。跨度元素有什么不同吗?
    • 不,它们和 span 没什么不同,在这里试试 --&gt; jsfiddle.net/E3kZy
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2022-01-01
    • 2019-12-02
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多