2016-10-14 8 views
1

テーブル内のいくつかの選択に基づいてPHPセッション変数を更新する単純なAJAXリクエストがあります。 AJAXリクエストでエラーが発生した場合は、選択した表の行の色を変更します(通常は「はい」を選択すると緑に変わり、「いいえ」を選択すると赤に変わります)。これはすべてうまくいきます。AJAX要求エラーでブートストラップアラートを表示

<div class="alert alert-warning alert-dismissible" role="alert"> 
 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span> 
 
    </button> 
 
    <strong>Warning!</strong> Better check yourself, you're not looking too good. 
 
</div>

が、私は私が発生する可能性がありますかわからない:

私は今、彼らの例のように、だけでなく、画面上のdismissibleブートストラップアラートのいずれかを表示したいと思いますこれは、AJAXリクエストにエラーがあるときに表示されます。

$this.closest('tr').addClass("warning"); 

が、私は拡張するかどうかはわかりません:そこのエラーかどう

$(document).ready(function() { 
 
    $('button.btn-success').click(function() { 
 
    var refreshItemID = $(this).closest('tr').attr('id'); 
 
    console.log(refreshItemID); 
 
    // Create a reference to $(this) here: 
 
    $this = $(this); 
 
    $.post('updateSelections.php', { 
 
     refreshItemID: refreshItemID, 
 
     selectionType: 'yes' 
 
    }, function(data) { 
 
     data = JSON.parse(data); 
 
     if (data.error) { 
 
     console.log(data); 
 
     console.log('something was wrong with the ajax request'); 
 
     $this.closest('tr').addClass("warning"); 
 
     return; // stop executing this function any further 
 
     } else { 
 
     console.log('update successful - success add class to table row'); 
 
     $this.closest('tr').addClass("success"); 
 
     $this.closest('tr').removeClass("danger"); 
 
     //$(this).closest('tr').attr('class','success'); 
 
     } 
 
    }).fail(function(xhr) { 
 
     console.log('ajax request failed'); 
 
     // no data available in this context 
 
     $this.closest('tr').addClass("warning"); 
 
    }); 
 
    }); 
 
});

あなたはそれを見ることができますが、テーブルの行への警告クラスを追加します。ここに私のスクリプトですこれはまた、棄却可能なアラートを追加する(そしてそのアラートの位置を制御する方法)?

+0

使用このブートストラップは、通知[こちらをクリック](http://bootstrap-notify.remabledesigns.com/) –

答えて

0

など。表の上または下にアラートのHTMLコードを追加し、style="display:none"をアラート部門に追加します。また、そのdivにid="alert_ajax_error"のようなIDを設定します。

今、失敗したコールバックは、次のようになりますなしとすることができますAJAX呼び出しの成功/ファイル/エラー内部:$("#alert_ajax_error").hide();

1

設定アラートのdivデフォルトの表示と成功の非表示、アラートの場合

.fail(function(xhr) { 
    console.log('ajax request failed'); 
    // no data available in this context 
    $this.closest('tr').addClass("warning"); 

    //make alert visible 
    $("#alert_ajax_error").show(); 
}); 

を以下のコードを使用して警告を表示します。

$(document).ready(function() { 
 

 

 
    $('button#alertshow').on('click', function() { 
 
    var msg_type = $("#msgtype").val(); 
 
    ShowAlert(msg_type, 'Message Content', msg_type); 
 
    }); 
 

 

 
    function ShowAlert(msg_title, msg_body, msg_type) { 
 
    var AlertMsg = $('div[role="alert"]'); 
 
    $(AlertMsg).find('strong').html(msg_title); 
 
    $(AlertMsg).find('p').html(msg_body); 
 
    $(AlertMsg).addClass('alert-' + msg_type); 
 
    $(AlertMsg).show(); 
 
    } 
 

 
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="alertshow">Show alert</button> 
 
<select id="msgtype"> 
 
    <option value="warning">Warning</option> 
 
    <option value="info">Info</option> 
 
    <option value="success">Success</option> 
 
    <option value="danger">Danger</option> 
 
</select> 
 
<div class="alert alert-dismissible" role="alert" style="display:none;"> 
 
    <strong>Warning!</strong> 
 
    <p>Better check yourself, you're not looking too good.</p> 
 
</div>

関連する問題