首页 > 解决方案 > 如果语句不起作用 / Javascript / Jquery

问题描述

我不知道为什么我的网站没有通过 if 语句。目标是在我单击按钮时显示图像,但它卡住了。如果有人可以帮助我会很高兴:D

function showllum() {
        $.get('llum.txt', function(data) {
            alert(data);
            if (data === "Llum") {
                    alert("1");
                    $('#showdada').empty();
                    $('#showdada').prepend($('<img>', {
                            id: 'llumpng',
                            src: 'llum.png'
                        }

                    ))
            } else if (data === "Molta Llum") {
                    alert("2");
                    $('#showdada').empty();
                    $('#showdada').prepend($('<img>', {
                            id: 'moltallumpng',
                            src: 'molta llum.png'
                        }

                    ))

                } else if (data === "Poca Llum") {
                        alert("3");
                        $('#showdada').empty();
                        $('#showdada').prepend($('<img>', {
                                id: 'pocallumpng',
                                src: 'poca llum.png'
                            }

                        ))

                    } else if (data === "Fosques") {
                            alert("4");
                            $('#showdada').empty();
                            $('#showdada').prepend($('<img>', {
                                    id: 'fosquespng',
                                    src: 'fosques.png'
                                }

                            ))

                        }})}

标签: javascriptjquery

解决方案


您需要添加一个数据类型 - http://api.jquery.com/jQuery.ajax/

$(document).ready(function() {
    $.ajax({
        url : "helloworld.txt",
        dataType: "text",
        success : function (data) {
            if(data === '*'){...}
        }
    });
}); 

但即使您更改为这个,您也不会从本地驱动器获得结果,您需要一个实际的 http 服务器来提供静态文本文件。请检查网络请求并在此处更新您得到的响应。


推荐阅读