另一个AJAX问题 [英] Another AJAX question

查看:54
本文介绍了另一个AJAX问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决我遇到的问题的清洁解决方案,我打电话给你的是什么,一个脏的问题。方式。


这是我想做的。我有一个下拉列表。单击下拉列表中的项目

将调用一个AJAX调用,该调用将获取数据,这些数据在我的屏幕的整个下半部分填充

。它使用innerHTML为

执行此操作,div标记包含所有这些。这很好用。


我还有一个编辑我想在下拉列表旁边显示的按钮,

但仅当所选项目满足某些条件时才会显示。在AJAX调用(具有db查询)之后,我不知道这些条件。


我试图实现这个的方式是在从AJAX调用编写的

区域中创建一个隐藏字段,在

AJAX调用期间将其值设置为0或1。返回后,在执行innerHTML之后,在相同的

javascript函数中,我查询该隐藏字段并设置包含编辑的范围的显示

属性。按钮为''''或者没有。


在页面的初始组成中,我选择了

下拉列表中的第一项并调用了内部代码生成内部的东西

div以便我的页面最初被填充。


这是问题所在。我在下拉列表中有两个项目。第一个

为隐藏字段生成0。第二个生成一个1.(我

已将print语句放入代码中,并且这些值正确计算为
。)但是,编辑按钮从不显示。所以,当显示第二个

时(它应该是1并显示编辑按钮),

我做了一个视图源。出现的是第一个显示的第一个

一个,值为0.


所以,这是我的问题。如何在AJAX

调用中传输信息,其他整个底部显示工作正常,以便我可以进行后处理以隐藏或显示编辑按钮。


我认为它可能是一个印记这个隐藏变量,但它不是
。我将这段代码从初始页面构建中移出,并且只在AJAX调用中出现了
。当然,隐藏变量根本没有显示

,因为视图源只显示了第一个未使用AJAX调用的
的结果。


我实施了一个相当脏的东西。方式,它的工作原理。我在AJAX之前使用0或1前置整个

返回。在返回时,我将

分开两个,然后将innerHTML与字符串start在第1位分开并使用

显示/隐藏的第一个值。但是,我想知道是否有更好,更清洁的方式。

I am looking for a clean solution to a problem that I solved in, what I
call, a "dirty" way.

Here is what I want to do. I have a dropdown list. Clicking on an item
in the dropdown list invokes an AJAX call that gets data which populates
the entire lower part of my screen. It does this with an innerHTML for
the div tag that holds all of this. This works fine.

I also have an "Edit" button that I want to show next to dropdown list,
but only if certain conditions are met for the item selected. I don''t
know those conditions until after the AJAX call (which has the db query).

The way I tried to implement this was to create a hidden field in the
area written from the AJAX call, with its value set to 0 or 1 during the
AJAX call. On return, right after doing the innerHTML, in the same
javascript function, I interrogate that hidden field and set the display
attribute of the span encompassing the "Edit" button to either '''' or none.

In the initial composition of the page, I selected the first item in the
dropdown list and called the inside code to generate the stuff inside
the div so that my page is initially filled.

Here is the problem. I have two items in the dropdown list. The first
one generates a 0 for the hidden field. The second generates a 1. (I
have put print statements into the code and those values are calculated
properly.) However, the edit button never shows. So, when the second
one is being displayed (where it should be 1 and show the edit button),
I did a view source. What appeared was the entire display for the first
one along with a value of 0.

So, here is my question. How can I transfer information within the AJAX
call, other that the entire bottom display which works fine, so that I
can do the post-processing to either hide or show the edit button.

I thought it might be an "imprinting" of this hidden variable, but it is
not. I moved that piece of code out of the inital page build and only
appearing in the AJAX call. Of course, the hidden variable didn''t show
up at all since the view source only showed the results for the first
one which did not use the AJAX call.

I implemented a rather "dirty" way and it worked. I prepend the entire
return from AJAX with either a 0 or a 1. On the return, I separate the
two, and then innerHTML with the string start at position 1 and use the
first value for show/hide. However, I was wondering if there is a
better, cleaner, way.

推荐答案

6月5日,1:下午36点,sheldonlg< sheldonlgwrote:
On Jun 5, 1:36 pm, sheldonlg <sheldonlgwrote:

我正在寻找一个解决我遇到的问题的干净解决方案,我打电话给b $ b ,脏的方式。


这是我想做的。我有一个下拉列表。单击下拉列表中的项目

将调用一个AJAX调用,该调用将获取数据,这些数据在我的屏幕的整个下半部分填充

。它使用innerHTML为

执行此操作,div标记包含所有这些。这很好用。


我还有一个编辑我想在下拉列表旁边显示的按钮,

但仅当所选项目满足某些条件时才会显示。在AJAX调用(具有db查询)之后,我不知道这些条件。


我试图实现这个的方式是在从AJAX调用编写的

区域中创建一个隐藏字段,在

AJAX调用期间将其值设置为0或1。返回后,在执行innerHTML之后,在相同的

javascript函数中,我查询该隐藏字段并设置包含编辑的范围的显示

属性。按钮为''''或者没有。


在页面的初始组成中,我选择了

下拉列表中的第一项并调用了内部代码生成内部的东西

div以便我的页面最初被填充。


这是问题所在。我在下拉列表中有两个项目。第一个

为隐藏字段生成0。第二个生成一个1.(我

已将print语句放入代码中,并且这些值正确计算为
。)但是,编辑按钮从不显示。所以,当显示第二个

时(它应该是1并显示编辑按钮),

我做了一个视图源。出现的是第一个显示的第一个

一个,值为0.


所以,这是我的问题。如何在AJAX

调用中传输信息,其他整个底部显示工作正常,以便我可以进行后处理以隐藏或显示编辑按钮。


我认为它可能是一个印记这个隐藏变量,但它不是
。我将这段代码从初始页面构建中移出,并且只在AJAX调用中出现了
。当然,隐藏变量根本没有显示

,因为视图源只显示了第一个未使用AJAX调用的
的结果。


我实施了一个相当脏的东西。方式,它的工作原理。我在AJAX之前使用0或1前置整个

返回。在返回时,我将

分开两个,然后将innerHTML与字符串start在第1位分开并使用

显示/隐藏的第一个值。但是,我想知道是否有更好,更清洁的方式。
I am looking for a clean solution to a problem that I solved in, what I
call, a "dirty" way.

Here is what I want to do. I have a dropdown list. Clicking on an item
in the dropdown list invokes an AJAX call that gets data which populates
the entire lower part of my screen. It does this with an innerHTML for
the div tag that holds all of this. This works fine.

I also have an "Edit" button that I want to show next to dropdown list,
but only if certain conditions are met for the item selected. I don''t
know those conditions until after the AJAX call (which has the db query).

The way I tried to implement this was to create a hidden field in the
area written from the AJAX call, with its value set to 0 or 1 during the
AJAX call. On return, right after doing the innerHTML, in the same
javascript function, I interrogate that hidden field and set the display
attribute of the span encompassing the "Edit" button to either '''' or none.

In the initial composition of the page, I selected the first item in the
dropdown list and called the inside code to generate the stuff inside
the div so that my page is initially filled.

Here is the problem. I have two items in the dropdown list. The first
one generates a 0 for the hidden field. The second generates a 1. (I
have put print statements into the code and those values are calculated
properly.) However, the edit button never shows. So, when the second
one is being displayed (where it should be 1 and show the edit button),
I did a view source. What appeared was the entire display for the first
one along with a value of 0.

So, here is my question. How can I transfer information within the AJAX
call, other that the entire bottom display which works fine, so that I
can do the post-processing to either hide or show the edit button.

I thought it might be an "imprinting" of this hidden variable, but it is
not. I moved that piece of code out of the inital page build and only
appearing in the AJAX call. Of course, the hidden variable didn''t show
up at all since the view source only showed the results for the first
one which did not use the AJAX call.

I implemented a rather "dirty" way and it worked. I prepend the entire
return from AJAX with either a 0 or a 1. On the return, I separate the
two, and then innerHTML with the string start at position 1 and use the
first value for show/hide. However, I was wondering if there is a
better, cleaner, way.



尝试

if(yourConditionIsMet){

thisElement.style.display =" block";

thisElement.style.visibility =" visible";

}

else {

thisElement.style.display =" none";

thisElement.style.visibility =" hidden";

}

Try
if (yourConditionIsMet){
thisElement.style.display = "block";
thisElement.style.visibility = "visible";
}
else {
thisElement.style.display = "none";
thisElement.style.visibility = "hidden";
}


sheldonlg写道:
sheldonlg wrote:

我正在寻找一个干净的解决方案来解决我遇到的问题,我打电话给你的是什么,一个脏的方式。
I am looking for a clean solution to a problem that I solved in, what I
call, a "dirty" way.



[snip]


你能提供脏代码吗?

[snip]

Could you provide your dirty code, please?


6月5日下午4:36,sheldonlg< sheldonlgwrote:
On Jun 5, 4:36 pm, sheldonlg <sheldonlgwrote:

所以,这是我的问题。如何在AJAX

调用中传输信息,其他整个底部显示工作正常,以便我可以进行后处理以隐藏或显示编辑按钮。
So, here is my question. How can I transfer information within the AJAX
call, other that the entire bottom display which works fine, so that I
can do the post-processing to either hide or show the edit button.



我会使用响应标头。在练习中,我经常使用

允许用于类似目的的标题。好处是这个标题

被代理服务器明确禁止修改,并且有用

来存储专有信息。在简单的是/否标志的情况下,

足以为允许编辑的内容发送常规集:

允许:HEAD,GET,POST

在这种情况下,此类标题的存在本身意味着是:

if !!(myXHR.getResponseHeader(''Allow'')){

//显示"编辑"按钮

}

也许明确的关键字会更加干净和安全:

允许:HEAD,GET,POST,EDITING
然后:

if(myXHR.getResponseHeader(''Allow'')。lastIndexOf(''EDI TING'')!= -1){

// show"编辑"按钮

}


修改服务器端脚本以发送

附加允许是一个琐事。可编辑内容的响应标题。

I would go with the response headers. On practice I am regularly using
"Allow" header for similar purposes. The benefits are that this header
is explicitly forbidden for modifications by proxy servers and useful
to store proprietary info. In the case of a simple yes/no flag it is
enough to send a regular set for the content allowed to be edited:
Allow: HEAD, GET, POST
In this case the presence itself of such header would mean "yes":
if !!(myXHR.getResponseHeader(''Allow'')) {
// show "Edit" button
}
Maybe an explicit keyword would be still more clean and secure:
Allow: HEAD, GET, POST, EDITING
and then:
if (myXHR.getResponseHeader(''Allow'').lastIndexOf(''EDI TING'') != -1) {
// show "Edit" button
}

It would be a trivia to modify your server-side script to send
additional "Allow" response header for editable content.


这篇关于另一个AJAX问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆