便利なjQuery機能

プロジェクトの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 .



  1. // $( "#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 .



  2. // $( "#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 .



  3. // $( "#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 .



  4. // $( "#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 .



  5. // $( "#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 .



  6. // $( "#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 .



  7. // $( "#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 .



  8. // $( "#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 .



  9. // $( "#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 .



  10. // $( "#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 .



  11. // $( "#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 .



  12. // $( "#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 .



  13. // $( "#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 .



  14. // $( "#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 .



  15. // $( "#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 .



  16. // $( "#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 .



  17. // $( "#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 .






All Articles