在没有iframe的情况下使用JavaScript提交表单/获取HTML?
上下文: 我在一个webapp上做学生工作,抄写纸质报告。它很旧,很遗憾,我们不能更改源代码,也不能直接运行DB查询 它只会在您提交整个表单后检查唯一ID是否存在,除非完全填写,否则无法提交。不用说,这是一个巨大的浪费时间,因为你经常抄写整个事情,却发现它是一个副本 目标: 我在下面创建了一个用户脚本,它启动了一个搜索,搜索唯一ID的输入(NOREFERENCEDECARANT)的onblur,检查是否有匹配项(行),并相应地返回。和Greasemonkey一起跑步。搜索表单位于同一域的另一页中。搜索表单不接受任何URL参数 不使用iframe(可能是AJAX?)可以做到这一点吗 这是一个提高我自身生产力的工具&同时学习JS。由于我还是一个初学者,欢迎提供任何让代码更干净的建议在没有iframe的情况下使用JavaScript提交表单/获取HTML?,javascript,jquery,ajax,iframe,userscripts,Javascript,Jquery,Ajax,Iframe,Userscripts,上下文: 我在一个webapp上做学生工作,抄写纸质报告。它很旧,很遗憾,我们不能更改源代码,也不能直接运行DB查询 它只会在您提交整个表单后检查唯一ID是否存在,除非完全填写,否则无法提交。不用说,这是一个巨大的浪费时间,因为你经常抄写整个事情,却发现它是一个副本 目标: 我在下面创建了一个用户脚本,它启动了一个搜索,搜索唯一ID的输入(NOREFERENCEDECARANT)的onblur,检查是否有匹配项(行),并相应地返回。和Greasemonkey一起跑步。搜索表单位于同一域的另一页中
//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();
}
//将函数添加到输入的模糊事件
$(文档)。在(“模糊”上,“无参考ECLARARANT”,是参考编号列表);
//瓦尔斯
var noReferenceDeclarant='';
var loadCode=0;
var$searchForm;
//浓汤
函数是refnumberexists()
{
NOREFERENCEDECARANT=$('#NOREFERENCEDECARANT').val();
loadCode=0;
//在继续之前,确保输入中有数据
if(无参考ECLARARANT)
{
//构建搜索iframe
$searchForm=$(''{
名称:'搜索窗口',
src:'rechercherGriIntranet.do?methode=presenterchercher',
id:“搜索窗口”,
宽度:0,
身高:0
}).附于(“主体”);
$searchForm.load(searchRefNumber);
}
}
函数searchRefNumber()
{
var isExists=false;
//检查它是哪个“加载”,以避免提交循环
如果(加载代码===0)
{
loadCode=1;
//用搜索词填写搜索表单
$(this.contentDocument).find('#norreference').val(norreferencedeclarant);
//设置搜索表单首选项
$(this.contentDocument).find('#typerappass').prop('checked',false);
$(this.contentDocument).find('#typerappas').prop('checked',false);
$(this.contentDocument).find('#typereportsi').prop('checked',true);
//提交表格
$(this.contentDocument).find('form:first').submit();
}
else if(loadCode==1)
{
载荷代码=2;
//查看结果表中是否有tr。如果没有结果,则有AD但没有tr。
var foundReports=$(this.contentDocument).find('.resultRecherche tr').length;
如果(foundReports>0)
{
if(确认('与此ID匹配的报告已存在。是否显示?'))
{
//在iframe中加载报表的模式窗口。尚未完成,但这相当简单。
}
其他的
{
//关闭并返回表单。
}
}
}
//重置变量/清除资源
删除$searchForm;
$(“#DateRedActionReport”).focus();
}
总的来说,我见过非常糟糕的代码
Ajax可以做到这一点,但是您只需要将Ajax响应放入DOM中(最有可能是作为iframe)
在这种情况下,我会保持你的方法。我认为这是最明智的
如果没有完整的上下文,可能会有一种方法来清理加载代码——但是您所拥有的内容是相同的,并且可以正常工作。很多人会称之为信号量,但这只是术语问题
我唯一真正需要澄清的是,建议不要频繁调用jQuery对象
// Many folks recommend that jQuery variables be named $<something>
var $doc = $(this.contentDocument);
doc.find('#typeRapportAss').prop('checked', false);
$doc.find('#typeRapportAS').prop('checked', false);
$doc.find('#typeRapportSI').prop('checked', true);
然后将其迭代到(在中使用和.hasOwnProperty)中的
此项目不需要,您正在做的很好,但这可能是一个学习练习。谢谢。很抱歉我的回答延迟了,我休假了:)loadCode的需要是检查我已经达到了哪一步,否则每次加载iframe时都会执行onload操作,从而导致loop、 我认为使用数字就可以很清楚地知道已经达到了哪一步。感谢关于信号量的说明,学习正确的术语/语义肯定是游戏的一部分!我会自己花一些时间来读一读。所有其他内容都会得到适当的说明,并将很快实施。非常感谢您的帮助。
var formValues = {
typeRapportAs: false,
typeRapportAS: false,
typeRapportSI: true
};