[英]Using a Javascript Date Selector in Qualtrics survey - carry date to the survey field
I'm trying to input a date selector tool so that a survey respondent can input a date by clicking on the field, a calendar will pop up, they can select the date, and the date will be inputted to the Qualtrics question text box.我正在尝试输入一个日期选择器工具,以便调查受访者可以通过单击该字段来输入日期,会弹出一个日历,他们可以选择日期,然后将日期输入到 Qualtrics 问题文本框中。
Qualtrics has their own calendar tool available, but the issue is that the calendar is always visible. Qualtrics 有自己的日历工具可用,但问题是日历始终可见。 I only want the calendar to be visible when they either click the text box itself or a calendar icon (either would be fine).
我只希望日历在他们单击文本框本身或日历图标时可见(两者都可以)。 Here's the Qualtrics code:
这是 Qualtrics 代码:
Enter a date:
<link href="https://ajax.googleapis.com/ajax/libs/yui/2.9.0/build/calendar/assets/skins/sam/calendar.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/yui/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/yui/2.9.0/build/calendar/calendar-min.js"></script> <script>
Qualtrics.SurveyEngine.addOnload(function ()
{
var qid = this.questionId;
var calid = qid + '_cal';
var y = QBuilder('div');
$(y).setStyle({clear:'both'});
var d = QBuilder('div',{className:'yui-skin-sam'},[
QBuilder('div', {id:calid}),
y
]);
var c = this.questionContainer;
c = $(c).down('.QuestionText');
c.appendChild(d);
var cal1 = new YAHOO.widget.Calendar(calid);
cal1.render();
var input = $('QR~' + qid);
$(input).setStyle({marginTop: '20px',width: '150px'});
var p =$(input).up();
var x = QBuilder('div');
$(x).setStyle({clear:'both'});
p.insert(x,{position:'before'});
cal1.selectEvent.subscribe(function(e,dates){
var date = dates[0][0];
if (date[1] < 10)
date[1] = '0' + date[1];
if (date[2] < 10)
date[2] = '0' + date[2];
input.value = date[1] +'-'+date[2]+'-'+date[0];
})
});
</script>
Here's the calendar tool that I'd like to use, but I can't figure out how to get the date to feed into the Qualtrics field text box.这是我想使用的日历工具,但我不知道如何将日期输入到 Qualtrics 字段文本框中。
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
Your problem is that in Qualtrics $ refers to prototypejs, not jQuery.您的问题是在 Qualtrics 中 $ 指的是prototypejs,而不是 jQuery。 So, you need to do something like:
因此,您需要执行以下操作:
$j = jQuery.noconflict();
$j( function() {
$j( "#datepicker" ).datepicker();
} );
However, that may not work if the datepicker code is itself using $ to refer to jQuery.但是,如果 datepicker 代码本身使用 $ 来引用 jQuery,这可能不起作用。
I've used https://github.com/joshsalverda/datepickr successfully with Qualtrics.我已经在 Qualtrics 中成功使用了https://github.com/joshsalverda/datepickr 。
EDIT: To implement datepickr in Quatrics:编辑:要在 Quatrics 中实现 datepickr:
Add this JavaScript to the field to use a plain HTML date input (no jQuery required).将此 JavaScript 添加到字段以使用纯 HTML 日期输入(不需要 jQuery)。
Qualtrics.SurveyEngine.addOnload(function() {
var textInputs = this.questionContainer.querySelectorAll('input[type="text"]');
if (typeof textInputs[0] !== 'undefined') {
textInputs[0].type = 'date';
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.