Datepicker图标不会出现在输入文本框旁边

And*_*ith 4 html jquery-ui class datepicker jsfiddle

正如您在下面的JS Fiddle中看到的那样,我只能在其中一个输入框旁边显示小的Datepicker图标.我需要它出现在两者旁边.

但是,将"id"更改为"class"不起作用,导致两个图标都消失.

http://jsfiddle.net/Vzt39/

HTML:

           <title>jQuery UI Datepicker - Icon trigger</title>

      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>


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

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



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


CSS:


  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />


  <link rel="stylesheet" href="/resources/demos/style.css" />
Run Code Online (Sandbox Code Playgroud)

jot*_*aen 6

您应该更改idclass,并更改$("#datepicker")$(".datepicker"),因为标识符应该是唯一/不同的.

然后,将jQuery代码包装起来$(document).ready(function(){ /*...*/ });以确保在执行javascript/jQuery代码时在DOM中创建html元素.

结果看起来像这样:

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

<p>Date: <input type="text" class="datepicker" /></p>
<p>Date: <input type="text" class="datepicker" /></p>
Run Code Online (Sandbox Code Playgroud)

这是一个工作小提琴:http://jsfiddle.net/GJHuf/1/