I have this code to create a HubSpot form. I am trying to redirect the user to a different page based on what option is selected in the HubSpot field called numemployees
.
我有这个代码来创建HubSpot表单。我正在尝试根据HubSpot字段中选择的名为NumEmployees的选项将用户重定向到不同的页面。
If the user selects the option of "1-150," the redirect URL should be X.
If the user selects any other option, the redirect URL should be Y.
如果用户选择“1-150”选项,则重定向URL应为X。如果用户选择任何其他选项,则重定向URL应为Y。
Here is my code. Portal ID, form ID, and redirect URLs are hidden here (fake info used). I used the "external embed" instructions on this page to create this code (I am not well-versed in JS).
这是我的代码。门户ID、表单ID和重定向URL在此隐藏(使用虚假信息)。我使用此页面上的“外部嵌入”说明来创建此代码(我不太熟悉JS)。
<script charset="utf-8" type="text/javascript" src="https://js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "123213213131",
formId: "123123213213213123123123123121",
onFormSubmit: function($form) {
var employeeCount = $form.find('select[name="numemployees"]').val();
setTimeout( function() {
if (employeeCount == "1-150" ) {
window.location.href = "https://www.url1.com";
}
else {
window.location.href = "https://www.url2.com";
}
}, 500 ); // Waits 1/2 second to redirect.
}
});
</script>
While testing, I can only get the redirect for the second URL here, even when I have selected "1-150" as the value for numemployees.
在测试时,我只能获得此处第二个URL的重定向,即使我选择了“1-150”作为NumEmployees的值。
I have a page where this form can be tested if need be, just lmk!
我有一个页面,这个表格可以测试,如果需要的话,只需lmk!
更多回答
can you console.log(employeeCount)
to see what value is being passed.
您是否可以对.log(EmploeCount)进行整合,以查看传递的值。
@Innovin Prefacing with I do not know JS well, so I wasn't sure where to place it. First I tried to paste it at the end, outside of the }); but before the closing script tag. When I do this, I get Uncaught ReferenceError: employeeCount is not defined
, which if I understand correctly is because the variable only exists inside the function, correct? When I place the console.log inside the function though, I don't see the value being passed, but is that because the function is only running on the submission of the form? I added the instructions I had followed to my original post!
@Innovin前言我不太了解JS,所以我不确定应该放在哪里。首先,我尝试将其粘贴到结尾处,位于})之外;但在结束脚本标记之前。当我这样做的时候,我得到了unautReferenceError:未定义EmploeCount,如果我理解正确的话,这是因为变量只存在于函数内部,对吗?但是,当我将console.log放在函数中时,我看不到传递的值,但这是因为该函数只在提交表单时运行吗?我将我遵循的说明添加到了我最初的帖子中!
can you share where this can be tested?
你能分享一下这项技术可以在哪里测试吗?
优秀答案推荐
The answer had less to do with my approach and more to do with the fact that HubSpot wasn't using jQuery. With some help from a co-worker, we changed
答案与我的方法关系不大,更多的是因为HubSpot没有使用jQuery这一事实。在同事的帮助下,我们改变了
var employeeCount = $form.find('select[name="numemployees"]').val();
Var Employee Count=$form.find(‘select[name=“numemployees”]’).val();
to
至
const employeeCount = $form.querySelector('select[name="numemployees"]').value;
常量员工计数=$form.querySelector(‘select[name=“numemployees”]’).value;
and that fixed the issue.
这就解决了这个问题。
更多回答
我是一名优秀的程序员,十分优秀!