0

我正在尝试从 jquery 为我的文档创建一条错误消息。

我已经<select>用 JSON 数据填充了一个菜单,它们链接到外部 HTML 文件以显示其位置的天气,如果该选项没有 HTML 文件,我需要显示一条错误消息。

For example the locations are London, New York, Paris and Rome, all except Rome have an HTML file that has weather data in it and displays fine but when Rome is selected...Nothing happens! 并且在选择另一个位置后选择罗马时,它会停留在当前数据上!

我正在使用 jQuery 来提取数据等。我的直觉是它需要一个if()语句,但我不确定语句的条件!

我的 jQuery 代码在这里...

$(document).ready(function () {

    // The below function pulls in the data from the external JSON file

    $.getJSON('json/destinations.json', function (data) {

        // attaches it to a variable
        var destinations = data.Destinations;

        $(destinations).each(function (id, destination) {
            $('#destinations').append('<option value="' + destination.destinationID + '">' + destination.destinationName + '</option>');
        });

        $("#destinations").change(function () {
            $('#weatherForecasts').load('raw_html/' + $(this).val() + '_weather.html .ngtable', function () {
                $('#weatherForecasts').show("slow");
            });
        });

    });

    // Hide statements for our extra fields and also the weather forecast DIV
    $('#weatherForecasts').hide();
    $('#extraFields').hide();
    $('.errorMessage').hide();

    // Function that allows us to see the extraFields when a radio button is checked!
    $("input[name='survey1']").change(function () {
        $("#extraFields").show("slow");
    });
    $("input[name='survey1']:checked").change(); //trigger correct state onload

});
4

1 回答 1

0

http://api.jquery.com/load/

在页面底部有一个处理错误的示例:

$( "#success" ).load( "/not-here.php", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});

所以在你的情况下

 $("#destinations").change(function () {
     $('#weatherForecasts').load('raw_html/' + $(this).val() + '_weather.html .ngtable', function (response, status, xhr) {
          if (status == 'error'){
             // do error things
          }else{
             $('#weatherForecasts').show("slow");
          }
     });
 });
于 2013-09-16T18:26:28.540 回答