如何从onchange获取值?

How to get value from onchange?

您好,如果从另一个输入字段(日期)将日期设置为今天,我基本上是在尝试禁用另一个输入字段。我似乎无法将 onchange 强加到输入字段并将该值获取到变量中以使其工作。

我已经成功地将 onchange(this) 放到输入字段中,但我必须将其取出并强制将 JS onchange 添加到它上面。

我试过下面的代码。如您所见,它应该在时间超过上午 10 点并且日期选择为今天时执行某些操作:

<script>
    var date = new Date();
    var dd = date.getDate();
    var mm = date.getMonth()+1;
    var yyyy = date.getFullYear();
    var hrs = date.getHours();

    if(dd<10) {
        dd='0'+dd
    } 

    if(mm<10) {
        mm='0'+mm
    }

    date = yyyy+'-'+mm+'-'+dd;


    var userDateEntry = document.getElementById("fm_deliverydate").onchange().value;

    if (hrs >= 10 && userDateEntry == date) {
        alert("Shipping for today has ended. Your order will arrive tomorrow");
        document.getElementById('fm_deliverytime').disabled= true;
    }
    else{
        document.getElementById('fm_deliverytime').disabled= false;
    }
</script>

<input  type="date" name="DateName" id="fm_deliverydate" />

<input  type="time" name="TimeName" id="fm_deliverytime" />

您没有正确附加活动。您使用 addEventListener.

附加一个事件

Fiddle: http://jsfiddle.net/AtheistP3ace/125jnvwp/1/

var date = new Date();
var dd = date.getDate();
var mm = date.getMonth() + 1;
var yyyy = date.getFullYear();
var hrs = date.getHours();

if (dd < 10) {
    dd = '0' + dd
}

if (mm < 10) {
    mm = '0' + mm
}

date = yyyy + '-' + mm + '-' + dd;

var userDateEntry = document.getElementById("fm_deliverydate");
userDateEntry.addEventListener('change',
    function () {
        if (hrs >= 10 && userDateEntry.value == date) {
            alert("Shipping for today has ended. Your order will arrive tomorrow");
            document.getElementById('fm_deliverytime').disabled = true;
        } else {
            document.getElementById('fm_deliverytime').disabled = false;
        }
    }
);

尝试将事件侦听器添加到该元素,并在它发生变化时触发您的函数。在 jsFiddle 上试用。您还可以考虑添加输入日期低于当前日期的情况。截至目前,您可以输入过去的日期,但仍然可以输入时间。

<input  type="date" name="DateName" id="fm_deliverydate" />
<input  type="time" name="TimeName" id="fm_deliverytime" />

<script>
var date = new Date();
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
var hrs = date.getHours();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
}

date = yyyy+'-'+mm+'-'+dd;

(function(){
    document.getElementById("fm_deliverydate").onchange =  function() {
       dateEntered();
    }
})();

function dateEntered(){
    var userDateEntry = document.getElementById("fm_deliverydate").value;

    if (hrs >= 10 && userDateEntry == date) {
        alert("Shipping for today has ended. Your order will arrive tomorrow");
        document.getElementById('fm_deliverytime').disabled= true;
    }
    else{
        document.getElementById('fm_deliverytime').disabled= false;
    }
}
</script>

如果您想将 onclick 事件侦听器添加到 html,那么您实际上只需要删除附加在 dateEntered 函数中的 onchange 方法并将其移动到 html,什么都不需要else 然后改变。

var date = new Date();
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
var hrs = date.getHours();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
}
date = yyyy+'-'+mm+'-'+dd;

function dateEntered(){
    var userDateEntry = document.getElementById("fm_deliverydate").value;

    if (hrs >= 10 && userDateEntry == date) {
        document.getElementById('fm_deliverytime').disabled = true;
    } else {
        console.log('shipping enabled');
        document.getElementById('fm_deliverytime').disabled = false;
    };
}

您的 html 看起来像这样:

<input  onchange="dateEntered()" type="date" name="DateName" id="fm_deliverydate" placeholder="yyyy-mm-dd"/>

<input  type="time" name="TimeName" id="fm_deliverytime" placeholder="enter date"/>

这是 fiddle:https://jsfiddle.net/o73fze9r/