简体   繁体   English

设置值

[英]Set the value of <input type=“date”… in jquery

Don't understand why this isn't working. 不明白为什么这不起作用。 I have a simple 'input type="date"' field as such.... 我有一个简单的'input type =“date”'字段......

<input type="date" name="Date"/>

And I'm trying to set the value to todays date whenever the page loads with this function... 每当页面加载此函数时,我都会尝试将值设置为今天的日期...

function setDate(date){
    z=$(date).attr('value');

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd} 
    if(mm<10){mm='0'+mm} 
    today = yyyy+'-'+mm+'-'+dd;     

    $(date).attr('value',today);
}

I've done the normal debugging and I know this function is being called and I know that the variable 'today' does in fact hold todays date in the form 'yyyy-mm-dd'. 我已经完成了正常的调试,我知道这个函数正在被调用,我知道变量'today'实际上确实以'yyyy-mm-dd'的形式保存今天的日期。 I have tried doing all different types of date formats (dd/mm/yyyy, dd-mm-yyyy, etc.) 我尝试过所有不同类型的日期格式(dd / mm / yyyy,dd-mm-yyyy等)

Any idea why this isn't working? 知道为什么这不起作用吗?

For input just use .val() 输入只需使用.val()

To read the value 阅读价值

  z=$(date).val();

To set the value 设置值

$(date).val(today);
$('input[name=Date]').val(today);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM