第二次调用JQuery脚本失败

时间:2013-08-28 07:25:03

标签: jquery html

我在以下代码中调用了两次jQuery函数,但它似乎只是第一次工作。

<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Datepicker - Icon trigger</title>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"
        />
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css" />
        <script>
            $(function () {
                $("#datepicker").datepicker({
                    showOn: "button",
                    buttonImage: "images/calendar.gif",
                    buttonImageOnly: true
                });
            });
        </script>
    </head>

    <body>

以下部分完美运作:

<p>Depature date:
    <input type="text" id="datepicker" />
</p> 

下一部分会生成输入文本框,但没有用于打开日历选项的图像链接[它不会调用脚本]:

    <p>Depature date:
        <input type="text" id="datepicker" />
    </p>**

    </body>

</html>

任何建议都会非常感激。我觉得这有一个简单的答案,我似乎无法看到它。

由于

4 个答案:

答案 0 :(得分:1)

元素should be unique的id,如果您有多个元素,则使用类属性

<p>Depature date: <input type="text" class="datepicker" /> 
</p> 
<p>Depature date: <input type="text" class="datepicker" /> 
</p>** 

然后

$(function() {
 $( ".datepicker" ).datepicker({
  showOn: "button",
  buttonImage: "images/calendar.gif",
  buttonImageOnly: true
 });
 });

演示:Fiddle

答案 1 :(得分:1)

不要在单个页面上使用多个ID。请改用类。

<p>Depature date: <input type="text" class="datepicker" /></p>
<p>Depature date: <input type="text" class="datepicker" /></p>   

答案 2 :(得分:1)

将id设为class.Id必须是唯一的。类是groups。我已经更改了你的代码并粘贴在这里..使用它可以工作..

<script>
    $(function () {
        $(".datepicker").datepicker({
            showOn: "button",
            buttonImage: "images/calendar.gif",
            buttonImageOnly: true
        });
    });
</script>

<p>Depature date:
    <input type="text" class="datepicker" />
</p>


<p>Depature date:
    <input type="text" class="datepicker" />
</p>

标记为答案并在问题解决后放弃投票。

答案 3 :(得分:1)

使用class而不是id。你不能在html页面中使用多个id