<!DOCTYPE html> <html lang="ja"> <head> <{$content_type|smarty:nodefaults}> <link rel="stylesheet" href="<{$root_surl}>css/master.css" type="text/css"> <!--<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> var select_id = 0; //--- ラジオボタン function quesFunc(){ if(select_id == this.id) return; $(this).addClass("selected"); $("#" + select_id).removeClass("selected"); select_id = this.id; var str = $("#" + select_id).text(); $(".nextBtn input[name='cselect']").val(select_id); $(".qSelect").text(str); ajaxQuestion(); } //--- ajax function ajaxQuestion(){ var id = '<{$xid}>'; $.ajax({ type: 'post', url: 'ajax_question.php', data: {id:xid, selectid:select_id}, success: function(data){ } }); } $(function(){ //---回答処理 $("ul>li").click(quesFunc); }); </script> </head> <body> <ul> <li id="1">aaaaa</li> <li id="2">bbbbb</li> <li id="3">ccccc</li> </ul> <form class="nextBtn" action="<{$app_url}>" method="post" target="_self"> <input type="hidden" name="cselect" value=""> <input type="submit" value="次へ"> </form> </body> </html>
▽1
●
a-kuma3 ●100ポイント ベストアンサー |
そこで、$.ajax の処理が完了するまで form の submit ボタンをグレーアウトにし、完了したらアクティブにするというのはできますか?
あと、通信エラーなどの処理でアドバイスがあればよろしくお願いします。
まずは、ajaxQuestion() 関数をちょっといじってみましたので、それを見てください。
function ajaxQuestion(){ // submit ボタンを無効化しておく $(".nextBtn input[type='submit']").attr("disabled", true); // ★ var id = '<{$xid}>'; $.ajax({ type: 'post', url: 'ajax_question.php', data: {id:xid, selectid:select_id}, success: function(data){ // 成功したら submit ボタンを有効にする $(".nextBtn input[type='submit']").attr("disabled", false); // ★ }, // ← カンマをつけてます error: function(jqXHR, status, error) { // ここにエラー処理を書きます // エラーのときもsubmit ボタンを有効にしておく $(".nextBtn input[type='submit']").attr("disabled", false); // ★ } }); }
submit ボタンの無効化については、お尻に「★」をつけた行を見てください。
エラー処理については、$.ajax() のパラメータで error を指定することで行います。
パラメータの意味についてはマニュアルをご覧ください。
settings の中の error の項目です。
http://api.jquery.com/jQuery.ajax/
基本的に、error で指定した処理に渡るときは、にっちもさっちも行かなくなっていると思うので、status の値を見ずに「ごめんなさい」だけでも良いような気もします。
2. 以下2つは何か違うのですか?どっちを書いても動きます。今のところ。
同じものです。
code.jquery.com の方が本家なので、新しいバージョンのものは、こちらから先にアクセスできるようになると思います。
ajax.googleapis.com の方は、Google がホスト(提供)してくれているものです。
Google は、jQuery の他にも、色々なライブラリをホストしてくれています。
https://developers.google.com/speed/libraries/devguide?hl=ja