我正在尝试在 Zendesk 中获取一个表单,该表单在提交时由自定义表单字段填写主题和描述。到目前为止,我已经尝试了两种方法来尝试这样做,但我都没有成功。
我通过 document_head.hbs 向站点添加了 JQuery,并使用了与推荐的类似的代码。 这是上面的来源
$('.form-field.string.optional.request_subject').hide();// Hide subject
$('.form-field.string.required.request_subject').hide(); // Hide subject
$('.form-field.request_description').hide(); // Hide description
$('#request_subject').val('test subject'); // Autofill subject
$('#request_description').val('test description'); // Autofill description
我正在尝试用自定义字段值替换描述和主题,因此我遵循了帖子中另一个人所说的建议。
$('.form-field.string.optional.request_custom_fields_360000410968').hide();// Hide Custom Field
因此,我的代码要考虑到这几处更改。
$('.form-field.string.required.request_subject').hide(); // Hide subject
$('.form-field.request_description').hide(); // Hide description
$('#request_subject').val('.form-field.string.required.request_custom_field_1500001768241'); // Autofill subject
$('#request_description').val('.form-field.string.required.request_custom_field_1500001768241'); // Autofill
使用此代码,描述值仍然可见,而不是使用用户插入到自定义字段中的值填充描述,而是插入一个字符串。
form-field.string.required.request_custom_field_1500001768241
由于我无法隐藏该字段,并且在获取另一个值时遇到问题,我尝试了方法二。
此方法涉及使用更多网站功能。有一些建议涉及使用 HTTP 或 URL 扩展。
我创建了一个 HTTP 扩展来 PUT 到这个 URL
https://*My Subdomain*.zendesk.com/api/v2/tickets/{{ticket.id}}.json
此扩展由站点触发器触发,该站点触发器在创建票证时激活。当触发器激活时,它会运行一个 JSON 语句。 在我放的 JSON 中
{"ticket": {"subject": "{{ticket.ticket_field_1500001768241}}"}}
这也不起作用。我不确定还需要采取哪些其他步骤,因此如果您能提供任何帮助,我将不胜感激。谢谢。
答案 0 :(得分:1)
为了确保代码在页面加载完成后运行,请将其添加到主题中的 script.js 中,在 'DOMContentLoaded' 事件侦听器内,如下所示:>
document.addEventListener('DOMContentLoaded', function() {
$('.form-field.string.optional.request_subject').hide();
$('.form-field.string.required.request_subject').hide();
...
将一个字段的值分配给另一个字段
您在这里所做的是将字符串传递给 .val() 方法。为了传递另一个字段的值,您应该使用以下语法:
$('#request_subject').val($('#request_custom_fields_1500001768241').val())
有关如何使用 .val() 的更多详细信息,请参阅 https://api.jquery.com/val/
添加事件处理程序
最后,如果你只是将上面的代码添加到你的 script.js 中,它只会在页面加载时运行一次,而不会在用户输入自定义字段或提交表单时运行。为了解决这个问题,请将前一行代码包装在一个事件处理程序中,然后每次发生特定事件(例如字段的值更改)时,它都会再次运行该代码。
$('#request_custom_fields_1500001768241').on('change', function() {
$('#request_subject').val($('#request_custom_fields_1500001768241').val());
$('#request_description').val($('#request_custom_fields_1500001768241').val());
});