简体   繁体   中英

two datepicker and why am i getting syntax error?

I had made code of two datepicker in which second datepicker's date will be always greater than first's. here i am giving code

<script>
   $(function() {
      jQuery.datepicker.setDefaults({dateFormat:"dd-mm-yy"});  
      $.datepicker.parseDate('dd-mm-yy', '20-09-2011');
      $( "#datepicker" ).datepicker({
        buttonImageOnly: false,
        changeMonth: true,
        changeYear: true,
        maxDate: '+36m+2w',
        minDate: new Date(),
        showAnim: '',

        onClose: function( selectedDate ) {
              $( "#rdatepicker" ).datepicker( "option", "minDate", selectedDate );
        }
     });
     $( "#rdatepicker" ).datepicker({
     buttonImageOnly: false,
     changeMonth: true,
     changeYear: true,
     maxDate: '+36m+2w',
     minDate: new Date(),
     showAnim: ''
  });

});
</script>

and i have error

SyntaxError: missing ) after argument list };

use class attribute instead of id - .datepicker instead of #datepicker

Edit:

When you need to use the same specific selector more than once, you should use classes because:

  • Classes can be used as many times as needed within a document.
  • IDs can only be applied once within a document.

Use a class instead of id, which works fine: replace # with .

<script>
 $(function() {
  jQuery.datepicker.setDefaults({dateFormat:"dd-mm-yy"});  
  $.datepicker.parseDate('dd-mm-yy', '20-09-2011');
  $( ".datepicker" ).datepicker({
    buttonImageOnly: false,
    changeMonth: true,
    changeYear: true,
    maxDate: '+36m+2w',
    minDate: new Date(),
    showAnim: '',

    onClose: function( selectedDate ) {
          $( "#rdatepicker" ).datepicker( "option", "minDate", selectedDate );
    }
 });
 $( ".rdatepicker" ).datepicker({
 buttonImageOnly: false,
 changeMonth: true,
 changeYear: true,
 maxDate: '+36m+2w',
 minDate: new Date(),
 showAnim: ''
    });

});
</script>

Your code runs fine.

Here is a fiddle. http://jsfiddle.net/F2BBN/

Your mistake should be somewhere else. It seems that you are missing a closing bracket in your rest of your js code.

Even with same inputs (ids) it should run with no syntax error http://jsfiddle.net/F2BBN/1/ .

<input type="text" id="datepicker"/>
<br/>
<input type="text" id="datepicker"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM