プロジェクトの1つでは、次のタスクがありました。ページにはいくつかのドロップダウンリストがあり、その内容はajaxを使用してロードされ、別のドロップダウンリストで選択した値に依存します(「メイン」と呼びます)。 Ajaxリクエストは、メインドロップダウンリストの変更イベントによってトリガーされます。
同期Ajaxリクエストでは、すべてのドロップダウンリストを調べて、リクエストを実行し、リストにコンテンツを挿入しました。 しかし、これにより遅延が発生しました。 非同期ajaxリクエストに切り替えると、問題が発生しました。このリクエストが実行されたリストをどのように見つけるかです。
これには他の解決策もあるかもしれませんが、私は次のことに気付きました。
実験のために、オプションのリストでJQuery.ajax()関数に新しいプロパティを渡そうとしました。 this変数によって参照されるオブジェクトのsuccess関数で使用可能になることが判明しました。
例:
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .
// $( "#order-otdel" ).change( function () { var otdel = $( "#order-otdel option:selected" ).val(); $( ".employee" ).each( function (i){ $.ajax({ idx: i, // <--- type: "GET" , url: "get.php" , data: "action=get_employee&oid=" + otdel + "&jid=" + $( this ).attr( 'job_id' ), success: function (options){ var cur = $( '.employee' ).eq( this .idx); // <--- cur.empty(); cur.append(options); //... }); }); }); * This source code was highlighted with Source Code Highlighter .