简体   繁体   English

如何在两个不同的文本框中显示值

[英]How to display value on two different text box

I want to calculate total days between start_date and end_date. 我想计算起始日期和结束日期之间的总天数。 Here i have two text box which is one textbox for end_date and other one for total days between start_date and end_date.What i want to do is when user click the button , the end_date textbox filled with end date and total days text box filled with total days . 这里我有两个文本框,一个是end_date的文本框,另一个是start_date和end_date之间的总天数的文本框。我想做的是当用户单击按钮时,end_date文本框填充了结束日期,total days文本框填充了总计天 。 Can anyone help me with it ? 有人可以帮我吗?

Here's the code. 这是代码。

<html>
<head>

<script>
$(document).ready(function () {
    $('#txtDate').datepicker();
    $('#follow_Date').getDate();
    $('#dura').getValue();
    $('#mark').getValue();


});

function getdate() {
    var sd = document.getElementById('txtDate').value;
    var d = document.getElementById('dura').value;
    var m = document.getElementById('mark').value;

    var date = new Date(sd);
    var newdate = new Date(date);
    var duration = parseInt(d);
    var marking = parseInt(m);


    if ( marking === 1 )
    {
        newdate.setDate(newdate.getDate());
    }
    else if ( marking === 2 )
    {
         newdate.setDate(newdate.getDate() + duration);
    }
    else if ( marking === 3 )
    {
         newdate.setDate((newdate.getDate() + duration)+1);
    }


    var dd = newdate.getDate();
    var mm = newdate.getMonth()+1;
    var y = newdate.getFullYear();

    var someFormattedDate = y + '-' + mm + '-' + dd;
    document.getElementById('follow_date').value = someFormattedDate;

}
function getdays()
{
    var days = (follow_date.setdate(follow_date.getDate()) - newdate.setDate(newdate.getDate()));
}

</script>
</head>
<body>

<p>Date:
    <input id="txtDate" type="text" />
</p>
<p>Duration :
    <input id="dura" type="text" />
</p>

<p>Marks:
    <input id ="mark" type = "text"/>
</p>
<p>
  <input type="button" onclick="getdate()" value="Fill Follow Date" />
</p>
<p>End Date:
    <input id="follow_date" type="text" onkeydown="getdate()" />
</p>
<p>Days :
    <input id="days" type="text" onkeydown="getdays()" />
</p>

</body>
</html>

I'm not exactly sure what you want to do in the end, but there are many errors in your code. 我不确定到底要做什么,但是代码中有很多错误。

Your getdays function cannot access the variables from your getdate function, eg follow_date and newdate , you need to define global variables or pass them to your function as arguments in order to use them. getdays功能不能从您访问的变量getdate功能,如follow_datenewdate ,你需要定义全局变量,或者将它们传递给你的函数作为参数,以使用它们。

Also you're mixing jQuery functions with plain JavaScript (if you even have included jQuery).. 另外,您还将jQuery函数与纯JavaScript混合在一起(如果您甚至包括jQuery)。

This is a small idea what you need to do to display another variable as your input value, see the comment and added two lines: 这只是一个小主意,您需要做什么才能显示另一个变量作为输入值,请参见注释并添加两行:

function getdate() {
    var sd = document.getElementById('txtDate').value;
    var d = document.getElementById('dura').value;
    var m = document.getElementById('mark').value;

    var date = new Date(sd);
    var newdate = new Date(date);
    var duration = parseInt(d);
    var marking = parseInt(m);


    if ( marking === 1 )
    {
        newdate.setDate(newdate.getDate());
    }
    else if ( marking === 2 )
    {
         newdate.setDate(newdate.getDate() + duration);
    }
    else if ( marking === 3 )
    {
         newdate.setDate((newdate.getDate() + duration)+1);
    }


    var dd = newdate.getDate();
    var mm = newdate.getMonth()+1;
    var y = newdate.getFullYear();

    var someFormattedDate = y + '-' + mm + '-' + dd;
    document.getElementById('follow_date').value = someFormattedDate;

    // added this two lines, that will set the days to your days input element
    var days = (follow_date.setdate(follow_date.getDate()) - newdate.setDate(newdate.getDate()));
    document.getElementById('days').value = days;
}

But as mentioned there are likely more problems with the code, I suggest reading a good JavaScript guide and start with simple things first if you want to unterstand it better, it makes no sense to rewrite your code so it just works I guess. 但是如前所述,代码可能存在更多问题,如果您想更好地理解它,我建议您阅读一本好的JavaScript指南,并从简单的事情入手,重写代码没有任何意义,所以我认为它可以正常工作。

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

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