如何根据其内容可以隐藏或显示一个div根据、内容、div

2023-09-10 21:44:49 作者:香椿丛林

我试图隐藏/显示 DIV 基于Ajax响应。我真的不知道,有没有办法呢?

Hi I'm trying to hide/show a div based on an AJAX response. I really don't know, is there way for that?

如果响应是一个错误,显示错误文本,并显示出格(同格)。

If response is an error, show the error text and show the div (same div).

如果反应不是错误,显示出成功的文字和隐藏一个div(同格)。

If response is not an error, show the success text and hide a div (same div).

这就像Facebook的功能。

It's like facebook function.

我应该做的,与JavaScript函数(S)。

I should do that with javascript function(s).

修改 增加信息来自OP的重复问题:

EDIT Added information from OP's duplicate question:

有我的div的。

<div id="messages"></div>
<div id="edit-address-book">
codes to edit the address book , input type - text, select , textearea etc...
</div>

楼下有我的简单的Ajax请求之一。 这是Ajax库,我用 - > http://en.dklab.ru / lib中/ JsHtt prequest /

Down below there is one of my simple ajax requests. This is the ajax library , that I use -> http://en.dklab.ru/lib/JsHttpRequest/

 // Create new JsHttpRequest object.
 var req_update = new JsHttpRequest();
 // Code automatically called on load finishing.
 req_update.onreadystatechange = function() {
  if (req_update.readyState == 4) {
  document.getElementById("messages").innerHTML = req_update.responseJS.messages;
  document.getElementById("changed_address").value = req_update.responseJS.changed_address;
  }
 }

 document.getElementById("messages").innerHTML = req_update.responseJS.messages;

 req_update.responseJS.messages; // This is ajax respond part.

在地址簿中的div,当用户点击更新按钮有来自阿贾克斯的错误或成功的消息作出反应成消息的div。

On the address book div, when customer click on update button there comes error or success message from ajax respond into messages div.

例如:


    Error Messages:
    -----------------
    Your First Name must contain a minimum of 2 characters.
    Your Last Name must contain a minimum of 2 characters.
    Your Street Address must contain a minimum of 5 characters.
    Your City must contain a minimum of 3 characters.
    Your State must contain a minimum of 2 characters.

    Success Message:
    --------------------
    Your address book has been successfully updated. (

我已经试过它只是向下跌破,但没有奏效:

I've tried it simply down below, but it didn't work:

<script language="javascript" type="text/javascript">
if ( document.getElementById("messages").innerHTML == "<?php echo SUCCESS_MESSAGE; ?>" ) {
 hide_div('edit-address-book); //The div to hide the address book entries.
}
</script>

如果有一个建议/解决方案jQuery JavaScript库,我可以使用它。

if there is a suggest/solution in jquery javascript library, I can use it as well.

推荐答案

最后,我已经找到了解决方案。这将帮助用户,谁正在使用AJAX库JSHtt prequest( HTTP ://en.dklab.ru/lib/JsHtt$p$pquest/ )。 在这里,它是:

Finally I've found the solution. It will help users, who are using the AJAX library "JSHttpRequest" ( http://en.dklab.ru/lib/JsHttpRequest/ ). Here it is:

    document.getElementById("messages").innerHTML = req_update.responseJS.messages;
    document.getElementById("changed_address").value = req_update.responseJS.changed_address;

###替换为:

    document.getElementById("messages").innerHTML = req_update.responseJS.messages;

    if (req_update.responseJS.messages == "<?php echo SUCCESS_MESSAGE; ?>") {
        hide_div("edit-address-book");
    }

    document.getElementById("changed_address").value = req_update.responseJS.changed_address;
    }