javascript 未捕获的类型错误:无法设置未定义的属性“显示”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26406422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:55:21  来源:igfitidea点击:

Uncaught TypeError: Cannot set property 'display' of undefined

javascriptphpjqueryajax

提问by n00bstacker

I am in the process of migrating an existing platform to a new server. I am taking the opportunity to upgrade PHP ect and standardise/debug the code as the previous maintainers have had different standards.

我正在将现有平台迁移到新服务器。我正在借此机会升级 PHP 等并标准化/调试代码,因为以前的维护者有不同的标准。

I have opted for PHP version 5.4.33 for now, once I have managed to move everything over to mysqli I will look to go to a more recent version. I didnt think anything server side would make a difference to AJAX/JS? As far as I am aware is it not client side?

我现在选择了 PHP 5.4.33 版,一旦我设法将所有内容都转移到 mysqli,我将寻找更新的版本。我认为服务器端不会对 AJAX/JS 产生任何影响吗?据我所知,它不是客户端吗?

Since I have moved the code over I am having issues with AJAX/JS. I am not the greatest at AJAX/JS and could use some assistance please. Even though every submit works differently through the entire platform I do not want to remove the AJAX/JS that already exists. I will most likely use it as an opportunity to how to use it as it makes end user experience smoother.

由于我已经移动了代码,我遇到了 AJAX/JS 问题。我在 AJAX/JS 方面不是最出色的,可以使用一些帮助。即使每个提交在整个平台上的工作方式都不同,但我不想删除已经存在的 AJAX/JS。我很可能会将它作为如何使用它的机会,因为它可以让最终用户体验更顺畅。

Using Chrome to debug I am receiving the following error on clicking the Save button:

使用 Chrome 调试我在单击“保存”按钮时收到以下错误:

Uncaught TypeError: Cannot set property 'display' of undefined
email_user
onclick

This is the Save button code

这是保存按钮代码

<span id="loading" style="color: red; font-size: x-small; display: none; text-decoration: blink;">Loading... Please wait..</span><input type="button" value="Save" class="save" onclick="if(validate()){ email_user(); }" />

This is the function code for validate()

这是validate()的函数代码

function validate() {

    var errorString = "";

    if(isBlank(document.getElementById("forename").value)) {
        errorString += " - Please input a forename\n";
    }
    if(isBlank(document.getElementById("surname").value)) {
        errorString += " - Please input a surname\n";
    }
    if(isBlank(document.getElementById("company_name").value)) {
        errorString += " - Please select a company\n";
    }

    if(document.getElementById("username").value != "" || document.getElementById("password").value != "") {
        if(isBlank(document.getElementById("username").value)) {
            errorString += " - Please input a username\n";
        }
        if(isBlank(document.getElementById("password").value)) {
            errorString += " - Please select a password\n";
        }
    }

    //if not a solicitor then cases mandatory
    if(document.getElementById("company_role_type_id").value == 2) { 
        if(document.getElementById("other_view_if").value == "") {
            errorString += " - Please select who can view your cases\n";
        }
    }
    if(document.getElementById("company_role_type_id").value == 3) { 
        if(document.getElementById("other_view_ea").value == "") {
            errorString += " - Please select who can view your cases\n";
        }
    }

    if(errorString) {
        alert('Please correct the following items:-\n\n'+ errorString);
        return false;
    }
    else {

        return true;
    }
}

This is the function code for email_user()

这是 email_user() 的函数代码

function email_user(){

if(skip_email == true){ $('user').submit(); }

var url  = 'email_user.php';
var params = '?' + $('user').serialize() + '&from_edit=1';

$('loading').style.display = 'inline';

var myAjax = new Ajax.Request(
                    url,
                    {
                            method: 'get',
                            parameters: params,
                            onComplete: show_response
                    });

function show_response(this_request){

//alert(this_request.responseText);

    var reply = this_request.responseText.evalJSON(true);

    if(reply['status'] == false){ var blah = ''; }

    else{ alert(reply['message']); }

//$('loading').style.display = 'none';

    $('user').submit();

    }

}

Thinking about it, maybe it is more to do with the Apache version?? Just in case Apache version is 2.2.15.

想了想,也许更多的是和Apache版本有关??以防万一 Apache 版本是 2.2.15。

Any assistance you guys can give me will be greatly appreciated! If you need any more information please let me know.

你们能给我的任何帮助将不胜感激!如果您需要更多信息,请告诉我。

Kind Regards,

亲切的问候,

n00bstacker

n00bstacker

回答by Guillermo

As previously stated in comments, your code has some issues, your line (the one that is triggering the error, can be optimized in the following way:

如前所述,您的代码有一些问题,您的代码行(触发错误的代码行,可以通过以下方式进行优化:

$('#loading').css("display","inline"); //Selector is ok now... 

In the other hand, I also noticed that you have a second selector $('user') that won′t work. Remember that anything without a dot, or a sharp will be considered as an element selector (loading, and user elements, won′t exist in your document unless you created it.

另一方面,我还注意到您有第二个选择器 $('user') 不起作用。请记住,任何没有点或尖的东西都将被视为元素选择器(加载和用户元素,除非您创建它,否则不会存在于您的文档中。

Remember:

记住:

$("#myId") //id selector
$(".myClass") //class selector

If "user" is the form name, the code may work. Remember that you want to catch the form submit event.

如果“用户”是表单名称,则代码可能有效。请记住,您要捕获表单提交事件。

Regards,

问候,

Guillermo

吉列尔莫

回答by EvilZebra

Try changing $('loading')to $('#loading')?

尝试更改$('loading')$('#loading')?