更改字段属性

时间:2012-09-18 07:17:16

标签: javascript date text input datepicker

我正在尝试更改字段属性,因为我使用了很多与firefox和Internet Explorer兼容的输入类型日期,

我还想将输入类型日期更改为输入类型文本,并添加用于添加jquery datepicker的id。

我也完成了那段代码:

   <script type="text/javascript" language="javascript">
    function changefield(){
        document.getElementsByTagNAme("date").innerHTML = "<input id=\"datepicker\" type=\"text\"  tabindex=\"2\" />";

    }
    chengefield()
</script>

此外似乎没有用。

我在控制台中没有我的'sage错误。

得到我最大的尊重。

亲切的问候。

SP

2 个答案:

答案 0 :(得分:1)

function changefield(){
    // getElementsByTagName return a collection of elements (like an array)
    var inputs = document.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'date') {

            // change the input's type to text
            inputs[i].setAttribute('type', 'text');

            // id for each element must be unique
            inputs[i].setAttribute('id', 'datepicker' + i);

            inputs[i].setAttribute('tabindex', i+2);
        }
    }

}

答案 1 :(得分:1)

我想你正在尝试的是选择输入类型日期,而不是元素。 你谈到添加jquery datepicker,为什么不使用jQuery呢?

$("input[type=date]").replaceWith("<input type='text' class='mydatepicker'/>");

然后

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

但不要将“id”与可以匹配多个元素的函数一起使用,因为这将生成重复的id;改用类 另外,如果你想知道为什么不简单地改变输入类型,IE dones'n允许