简体   繁体   中英

JQuery datepicker function for multiple from inputs

I'm trying to use jquery's datepicker for multiple form fields which I have working when hard coded. I don't know how to do this for a variable number of fields. For example, below I have hard coded it to work for two inputs. I want to do this for any amount of inputs the user provides on a previous page. Thanks for the help!

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
      <script src="https://www.cvrc.virginia.edu/otherscripts/jquery-1.9.1.js"></script>
      <script src="https://www.cvrc.virginia.edu/otherscripts/jquery-ui.js"></script>
      <script>
      $(function() {
        $( "#datepicker" ).datepicker();
      });
      $(function() {
        $( "#datepicker2" ).datepicker();
      });
      </script>

      <input type=text name=date size=10 id=datepicker>
      <input type=text name=date size=10 id=datepicker2>

Use a common class or use the name with an attribute selector

JavaScript

$('[name="date"]').datepicker();

or

$(".datepicker").datepicker();

HTML if you go with the class selector

 <input type="text" name="date" size="10" id="datepicker" class="datepicker">
 <input type="text" name="date" size="10" id="datepicker2" class="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