如何使用For循环创建不同的ID名称。

时间:2012-10-30 18:42:37

标签: javascript

window.onload = function()
{   
new JsDatePick({
useMode:2,
target:"inputField1", //HERE I WOULD LIKE TO PASS DIFFERENT ID ex. "inputField1"+ "i"
dateFormat:"%Y-%M-%d",
yearsRange:[1978,2120],
limitToToday:false,
cellColorScheme:"beige",
imgPath:"main/img/",
weekStartDay:1
});
};

在目标替换inputField1我希望有inputField1,inputField2,inputField3等。

3 个答案:

答案 0 :(得分:1)

for(i=0;i<5;i++) {
    temp = "inputField"+i;
    window.onload = function() {   
        new JsDatePick({
            useMode: 2,
            target: temp,
            dateFormat: "%Y-%M-%d",
            yearsRange: [1978,2120],
            limitToToday: false,
            cellColorScheme: "beige",
            imgPath: "main/img/",
            weekStartDay: 1
        });
    };
}

答案 1 :(得分:0)

使用类作为输入字段,如class =“inputfield”,然后使用循环在javascript中创建html。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery_urdu_editor/jquery.js" type="application/javascript"></script>
<link rel="stylesheet" type="text/css" media="all" href="jsdatepick-calendar/jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsdatepick-calendar/jsDatePick.min.1.3.js"></script>
<script type="application/javascript">
 window.onload = function()
    {   
      $('.inputfield').each(function() {
         var thisId = $(this).attr('id');
            //document.write(thisId.'<br />');
         new JsDatePick({
             useMode:2,
             target:thisId,    // putting the id of this element.                    
             dateFormat:"%Y-%M-%d",
             yearsRange:[1978,2120],
             limitToToday:false,
             cellColorScheme:"beige",
             imgPath:"main/img/",
             weekStartDay:1
        });
    });
   };
</script>
</head>

<body>
<?php
    for($i=1;$i<=6;$i++){
        echo '<input type="text" name="textfield'.$i.'" class="inputfield" id="textfield'.$i.'" value="" ><br />';

    }
?>
</body>
</html>

在javascript代码中,我们获取了带有类的元素的id,然后将该ID放入Calender代码中。因此它适用于具有该类的任意数量的输入字段。 希望它可以帮助你。

答案 2 :(得分:0)

window.onload = function()
{   
var i = <?=$totalNumberOfInputs;?>

for(j=1;j<=i;j++) {  
    new JsDatePick({
        useMode:2,
        target:"inputField" + j, //HERE I WOULD LIKE TO PASS DIFFERENT ID ex. "inputField1" + j
        dateFormat:"%Y-%M-%d",
        yearsRange:[1978,2120],
        limitToToday:false,
        cellColorScheme:"beige",
        imgPath:"main/img/",
        weekStartDay:1
    });
}

}

这是我的另一个问题的答案谢谢