如何使用Javascript将日期插入文本框?

时间:2013-05-30 02:44:41

标签: javascript jquery html date

我想使用javascript将今天的日期放入文本框中;这是我的代码:

function add_event() {
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    var day1 = day + "." + month + "." + year

    var html = '<tr><td class="date"><input type="text" name="date_evnt"  value="?"></td> <td class="title"><input type="text" value="New Event"></td> <td class="delete"><input type="button" value="-"></td></tr>';
    $('#events-table').append(html);

    events_table_events();
}

我不确定如何将日期设置为文本框名称date_evnt

2 个答案:

答案 0 :(得分:3)

如果您只想将day1值放在date_evnt中,则可以执行此操作:

var html = '<tr><td class="date"><input type="text" name="date_evnt"  value="' + day1 + '"></td> <td class="title"><input type="text" value="New Event"></td> <td class="delete"><input type="button" value="-"></td></tr>';

答案 1 :(得分:0)

你可以试试这个

function add_event() {
    var currentDate = new Date(), day = currentDate.getDate(),
        month = currentDate.getMonth() + 1, year = currentDate.getFullYear(),
        day1 = day + "." + month + "." + year;

    var txt1 = $('<input/>', { 'name':'date_evnt', 'value':day1 }),
        tr = $("<tr/>"), td = $('<td/>');

    tr.append(td.attr('class','title').html('New Event '))
    .append(td.append(txt1))
    .append(td.append($('<button/>', {'html':'-'})));

    $('#events-table').append(tr);
}
add_event();

DEMO.