Передать данные javascript json с помощью jquery - PullRequest
0 голосов
/ 10 марта 2011

Я построил эту функцию:

function currentDate() {
    var date;
    var currentDate = new Date();
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();

    // if the month length = 1 add "0" to ir
    if (currentDate.getMonth().toString().length == 1) {
        month.toString();
        month = "0" + month;
    }

    // if the day length = 1 add "0" to ir  
    if (currentDate.getDate().toString().length == 1) {
        day.toString();
        day = "0" + day;
    }

    var ISO = year + "-" + month + "-" + day;
    var EUR = day + "/" + month + "/" + year;

    return date = {
        "ISO": ISO,
        "EUR": EUR
    };
}

Когда я нажимаю на кнопку, я хочу, чтобы в текстовый ввод передавалась дата ISO или евро.Как это можно сделать ???

Спасибо ..

Ответы [ 3 ]

1 голос
/ 10 марта 2011

Попробуйте этот пример скрипки

<button data-type="EUR">set date EUR</button>
<button data-type="ISO">set date ISO</button>
<input id="date" type="text" />

JS:

$('button').click(function(e){
    var d = currentDate(),
        type = $(this).data().type;
    $('#date').val(d[type]);
});
1 голос
/ 10 марта 2011
<html>
  <head>
    <Script>
      // your sript goes here
    </script>
  </head>
  <body>
    ...
    <input type="text" id="date" size="15" /><br />
    <input type="button" onclick="document.getElementById('date').value=currentDate().ISO;" value="ISO" />
    <input type="button" onclick="document.getElementById('date').value=currentDate().EUR;" value="EUR" />
    ,...
  </body>
</html>

ДЕМО

Или jQuery: ДЕМО

0 голосов
/ 10 марта 2011

При определении объекта ключ не обязательно должен быть в кавычках.

<input id="cdate" />
<input type="button" onclick="$('#cdate').val(currentDate().ISO)" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...