简体   繁体   English

将文本框的值放入另一个文本框

[英]Put value of textbox into another textbox

I have a start date which is put in a textbox by the user trough a jquery date picker. 我有一个开始日期,该日期由用户通过jquery日期选择器放置在文本框中。
I also have an end date textbox which I want to fill with the start date value when the user changed the value of the start date. 我还有一个结束日期文本框,当用户更改开始日期的值时,我想用开始日期值填充。

So I was thinking of doing a JavaScript and than a when textbox has changed enter value into end date textbox but I don't know how this would be done. 因此,我当时正在考虑使用JavaScript,而不是在文本框更改时,将值输入到结束日期文本框,但是我不知道该怎么做。

Thank you guys for helping since I am new to JavaScript. 谢谢大家的帮助,因为我是JavaScript新手。

Here is the working code for what you have asked. 这是您所要求的工作代码。

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>jQuery UI Datepicker - Select a Date Range</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.9.1.js"></script>
        <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
        <script>
            $(function() {
                $("#fromdate").datepicker({
                    onSelect: function() {
                        $(this).change();
                    }
                });
                $("#fromdate").change(function (){
                    var val=$("#fromdate").val();
                    $("#todate").val(val);
                });
            });
        </script>
    </head>
    <body>
        <label for="from">From</label>
        <input type="text" id="fromdate" name="from">
        <label for="to">to</label>
        <input type="text" id="todate" name="to">


    </body>
</html>

And working JSFiddle ( Here ) 和工作的JSFiddle在这里

If it is this calendar , (which it looks like from your own "answer" ), you could do something like this. 如果是此日历 (从您自己的“答案”看来),则可以执行以下操作。 It will set " date end " and pop up calendar for it when "date from" is selected: 当选择“ date from”时,它将设置“ date end ”并为其弹出日历:

window.onload = function(){
    var cal_start = new JsDatePick({
        useMode:2,
        target:"date_start",
        dateFormat:"%m/%d/%Y"
    });

    var cal_end = new JsDatePick({
        useMode:2,
        target:"date_end",
        dateFormat:"%m/%d/%Y"
    });
    cal_start.addOnSelectedDelegate(function(){
        document.getElementById('date_end').value = cal_start.getSelectedDayFormatted();
        cal_end.showCalendar();
    });
};

If you do not want the calendar-end to pop up, remove: 如果您不希望弹出日历末端,请删除:

cal_end.showCalendar();

Original answer as per your statement "[…] trough a jquery date picker […]" in question text. 根据您在问题文本中“通过jQuery日期选择器[…]的声明[[…]”的原始答案。

jQuery UI variant: jQuery UI变体:

Use onSelect event. 使用onSelect事件。

Simple demo : 简单的演示

$(function() {
    $("#date_start").datepicker({
        onSelect: function(d) {
            $("#date_end").val(d);
        }
    });
    $("#date_end").datepicker();
});

You could also add for change event on element, but not sure how user-friendly that would be. 您还可以在元素上添加for change事件,但不确定如何做到用户友好。

This is just the simple method below,but if you need something other,let me know 这只是下面的简单方法,但是如果您需要其他方法,请告诉我

function copytextbox()
{
 document.getElementById('textbox2').value=document.getElementById('textbox1').value;

return false;

}
  <script>
    function myfunction()
    {

    document.getElementById("text2").value=document.getElementById("text1").value;
}   
 </script>

and your start date textbox- 和您的开始日期文本框-

<input type="text" name="startdate" id="text1" onchange="myfunction()"/>
<input type="text" name="enddate" id="text2" />

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

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