jQuery Datepicker for multiple inputs

前端 未结 8 1754
忘了有多久
忘了有多久 2021-01-04 16:47

I have a jQuery datepicker script:

$(function() {
    $( \"#datepicker\" ).datepicker({ dateFormat: \"yyyy-mm-dd\" });         


        
相关标签:
8条回答
  • 2021-01-04 16:52
    <input type="text" name="MyDate1" id="datepicker1">
    <input type="text" name="MyDate2" id="datepicker2">
    

    Just get input fields using #id and apply datepicker.

    $(function() {
        $( "#datepicker1, #datepicker2" ).datepicker({ dateFormat: "yy-mm-dd" });
    });
    
    0 讨论(0)
  • 2021-01-04 16:56

    Just change all id to class.

    <input type="text" name="MyDate1" class="datepicker">
    <input type="text" name="MyDate2" class="datepicker">
    
    $(function() {
      $( ".datepicker" ).datepicker({ dateFormat: "yyyy-mm-dd" });
    }); 
    

    also can use

    $(function() {
      $( "input[name^=MyDate]" ).datepicker({ dateFormat: "yyyy-mm-dd" });
    });
    
    0 讨论(0)
  • 2021-01-04 16:59

    Tired of repeating this, but still, am repeating it once more. ids have to be unique. So, use this:

    <input type="text" name="MyDate1" class="datepicker">
    <input type="text" name="MyDate2" class="datepicker">
    

    with

    $(function() {
        $( ".datepicker" ).datepicker({ dateFormat: "yyyy-mm-dd" });
    });
    
    0 讨论(0)
  • 2021-01-04 17:04

    Access date picker by id

    <input type="text" name="MyDate1" id="datepicker1">
    <input type="text" name="MyDate2" id="datepicker2">
    
    $(function() {
      $( "input[id^=datepicker]" ).datepicker({ dateFormat: "dd-mm-yy" });
    });
    

    OR access date picker by name:

    <input type="text" name="MyDate1" id="datepicker1">
    <input type="text" name="MyDate2" id="datepicker2">
    
    $(function() {
       $( "input[name^=MyDate]" ).datepicker({ dateFormat: "dd-mm-yy" });
    });
    
    0 讨论(0)
  • 2021-01-04 17:08

    Access datepicker function by Name:

    $(function() {
        $('[name="MyDate1"]').datepicker({ dateFormat: "yyyy-mm-dd" });
    }); 
    
    0 讨论(0)
  • 2021-01-04 17:08

    You could also add classes on the fly if input fields are dynamically generated, such as in Django templates.

    <input type="text" name="MyDate1" id="datepicker1">
    <input type="text" name="MyDate2" id="datepicker2">
    

    Get input fields using #id and add a class

    $(function() {
        $( "#datepicker1, #datepicker2" ).addClass('datepicker');
        $( ".datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
    });
    
    0 讨论(0)
提交回复
热议问题