컨텍스트 : 나는 웹 응용 프로그램에서 종이 보고서를 필사하는 학생의 작업을합니다. 그것은 오래되었고 유감스럽게도 원본을 변경하거나 DB 쿼리를 직접 실행할 수 없습니다.양식 제출/iframe없이 JavaScript로 HTML 가져 오기?
전체 양식을 제출하고 나면 고유 ID가 존재하는지 확인하고 완전히 채워지지 않으면 제출할 수 없습니다. 말할 필요도없이, 모든 것을 복제하여 복제본이라는 것을 깨닫기 만하면 시간 낭비가됩니다.
목적 : 은 그 아래 userscript가 어떤 경기 (행)와이있는 경우, 검사 따라 반환 고유 ID의 입력의에 onblur (noReferenceDeclarant)에서 검색 검색 시작했다. 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();
}
감사합니다. 답변 지연에 대한 미안 해요, 나는 휴일에서 일하던 중이었습니다 :) loadCode의 필요성은 도달 한 단계를 확인하는 것입니다. 그렇지 않으면 iframe이로드 될 때마다 onload 액션이 실행되어 루프가 발생합니다. 수치를 사용하여 어떤 단계에 도달했는지 알 수 있습니다. – tsc
_semaphores_에 대한 메모를 보내 주셔서, 적절한 용어/의미를 배우는 것이 확실히 게임의 일부입니다! 나는 그것에 대해 조금 읽으려면 시간을 좀 할애 할 것입니다. 다른 모든 사람들은 정식으로 언급하고 곧 시행 할 것입니다. 귀하의 도움을 많이 주시면 감사하겠습니다. – tsc