【发布时间】:2013-12-13 20:12:14
【问题描述】:
背景: 我从事学生工作,在 web 应用程序中转录纸质报告。它很旧,很遗憾我们无法更改源,也无法直接运行数据库查询。
它只会在您提交整个表单后检查唯一 ID 是否存在,除非填满,否则无法提交。不用说,这是在浪费大量时间,因为您经常转录整个内容后才意识到它是重复的。
目标: 我在下面创建了用户脚本,它启动了对唯一 ID 输入(noReferenceDeclarant)的 onblur 的搜索,检查是否有任何匹配项(行)并相应地返回。与 Greasemonkey 一起运行。搜索表单位于同一域的另一个页面中。搜索表单不接受任何 URL 参数。
这可以在不使用 iframe 的情况下完成吗(也许是 AJAX?) 这是我自己的生产力和同时学习 JS 的工具。由于我仍然是一个初学者,因此欢迎任何使代码更清晰的提示。
//Adding function to input's blur event
$(document).on ("blur", "#noReferenceDeclarant", isRefNumberExists);
//Vars
var noReferenceDeclarant = '';
var loadCode = 0;
var $searchForm;
//Fonctions
function isRefNumberExists ()
{
noReferenceDeclarant = $('#noReferenceDeclarant').val();
loadCode = 0;
//Make sure there's data in the input before proceeding
if (noReferenceDeclarant)
{
//Build search iframe
$searchForm = $('<iframe />', {
name: 'searchWindow',
src: 'rechercherGriIntranet.do?methode=presenterRechercher',
id: 'searchWindow',
width: 0,
height: 0
}).appendTo('body');
$searchForm.load(searchRefNumber);
}
}
function searchRefNumber()
{
var isExists = false;
//Check which "load" it is to avoid submit loops
if (loadCode === 0)
{
loadCode = 1;
//Filling search form with search term
$(this.contentDocument).find('#noReference').val(noReferenceDeclarant);
//Set search form preferences
$(this.contentDocument).find('#typeRapportAss').prop('checked', false);
$(this.contentDocument).find('#typeRapportAS').prop('checked', false);
$(this.contentDocument).find('#typeRapportSI').prop('checked', true);
//Submit the form
$(this.contentDocument).find('form:first').submit();
}
else if (loadCode === 1)
{
loadCode = 2;
//See if there are any tr in the result table. If there are no results, there a thead but no tr.
var foundReports = $(this.contentDocument).find('.resultatRecherche tr').length;
if (foundReports > 0)
{
if (confirm('A report matching this ID already exists. Do you want to display it?'))
{
//Modal window loading the report in an iframe. Not done yet but that's fairly straightforward.
}
else
{
//Close and return to the form.
}
}
}
//Reset variables/clean ressources
delete $searchForm;
$('#dateRedactionRapport').focus();
}
【问题讨论】:
标签: javascript jquery ajax iframe userscripts