简体   繁体   English

输入表格的数据未保存

[英]Data entered into form not saved

I am trying to write a script that can generate insurance quotes for multiple postal codes from this site . 我正在尝试编写一个脚本,该脚本可以从该站点为多个邮政编码生成保险报价。 So far I've managed to enter data into the dialog. 到目前为止,我已经设法在对话框中输入数据。 Manually entering consists of: clicking the postal code field, a dialog opens, enter postal code and click "continue". 手动输入包括:单击邮政编码字段,将打开一个对话框,输入邮政编码,然后单击“继续”。

My script looks like this, so far: 到目前为止,我的脚本如下所示:

document.querySelector('.btn.btn-default.postal-code').click();
window.setTimeout(function() {
  document.getElementById("PostalCode").value = "K0A1B0";
}, 2000);
window.setTimeout(function() {
  document.querySelector('.btn.btn-orange.validate-postal-code').click();
}, 2000);

If I execute the first two lines, the dialog opens and the postal code gets entered correctly. 如果执行前两行,将打开对话框,并且邮政编码正确输入。

If I execute all of it, the postal code doesn't seem to change, but the dialog closes. 如果我全部执行,邮政编码似乎没有改变,但是对话框关闭。 However, the value is not saved in the field on the main page. 但是,该值未保存在主页上的字段中。 What am I doing wrong? 我究竟做错了什么?

I've done some searching around and tried submitting the form before closing, but don't really know what I need to do here. 我已经进行了一些搜索,并尝试在关闭之前提交表单,但是我真的不知道我需要在这里做什么。 Neither of these worked: 这些都不起作用:

document.forms[0].submit();
document.getElementById("PostalCode").submit();

When you use setTimeout , it schedules the execution at the time you specify and then continues executing the next line. 使用setTimeout ,它会在您指定的时间安排执行时间,然后继续执行下一行。 In your case, the code in the two setTimeout lines will be executed almost at the same time after 2 seconds. 在您的情况下,两行setTimeout的代码将在2秒后几乎同时执行。 You can use 4 second for the second one: 您可以将4秒用于第二个:

document.querySelector('.btn.btn-default.postal-code').click();
window.setTimeout(function() {
    document.getElementById("PostalCode").value = "K0A1B0";
}, 2000);
window.setTimeout(function(){
     document.querySelector('.btn.btn-orange.validate-postal-code').click();
}, 4000);

Alternatively, you can call the second one inside the first one, in which case the first one will be executed after 2 seconds and will schedule the second one to be executed after 2 more seconds: 或者,您可以在第一个中调用第二个,在这种情况下,第一个将在2秒后执行,并安排第二个在2秒后执行:

document.querySelector('.btn.btn-default.postal-code').click();
window.setTimeout(function() {
    document.getElementById("PostalCode").value = "K0A1B0";
    window.setTimeout(function() {
        document.querySelector('.btn.btn-orange.validate-postal-code').click();
    }, 2000);
}, 2000);

EDIT The value is validated and copied to the main page when the keyboard events are triggered. 编辑触发键盘事件时,将验证该值并将其复制到主页。 You can simulate that in JavaScript, but it is easier to do it in jQuery since the website uses it. 您可以在JavaScript中进行模拟,但由于该网站在使用jQuery,因此更容易在jQuery中进行模拟。 Add trigger("change") after you change the value and you will see the postal code validated and copied to the box on the main page: 更改值后添加trigger("change") ,您将看到邮政编码已验证并复制到主页上的框中:

document.querySelector('.btn.btn-default.postal-code').click();
window.setTimeout(function() {
  $("#PostalCode").val("K1A1B0").trigger("change");
}, 2000);
window.setTimeout(function() {
  document.querySelector('.btn.btn-orange.validate-postal-code').click();
}, 4000);

Note: if you use an invalid postal code in the code above, the value will change and get copied, but you will see the validation message and the popup will stay open. 注意:如果您在上面的代码中使用了无效的邮政编码,则该值将更改并被复制,但是您将看到验证消息,并且弹出窗口将保持打开状态。 If you want to manually validate the postal code, call the ValidatePostalCode() function. 如果要手动验证邮政编码,请调用ValidatePostalCode()函数。 It returns true or false , and based on that you can decide what to do next. 它返回truefalse ,然后您就可以决定下一步要做什么。

MAYBE... 也许...

This may be occurring cause you're setting the value, 这可能是由于您正在设置值而引起的,

window.setTimeout(function(){document.getElementById("PostalCode").value = "K0A1B0";}, 2000); window.setTimeout(function(){document.getElementById(“ PostalCode”)。value =“ K0A1B0”;},2000);

and also submitting it at the same instant... and that's the problem. 并同时提交...这就是问题所在。

window.setTimeout(function(){document.querySelector('.btn.btn-orange.validate-postal-code').click();}, 2000); window.setTimeout(function(){document.querySelector('。btn.btn-orange.validate-postal-code')。click();},2000年);

Try this out... 试试看...

var execute = function () {
  'use-strict';
   document.querySelector('.btn.btn-default.postal-code').click();
   window.setTimeout(function(){document.getElementById("PostalCode").value = "K0A1B0";}, 2000);
   window.setTimeout(function(){document.querySelector('.btn.btn-orange.validate-postal-code').click();}, 3000);
}();

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

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