简体   繁体   English

如何使用Ajax而不是文本框更新标签?

[英]can I update a label using ajax, instead of a textbox?

I'm using ajax to update a textbox once the value in a drop down box has been selected. 一旦选择了下拉框中的值,我将使用ajax更新文本框。 However, i only update/put text in it if certain conditions are met, so it looks rubbish when there is no message. 但是,如果满足某些条件,我只会在其中更新/放置文本,因此在没有消息时,它看起来很垃圾。 I was thinking that having a label there would be much better, but is this possible? 我当时想拥有一个标签会更好,但是这可能吗? Can i have an empty label and just update the label text when needed? 我可以有一个空标签,只在需要时更新标签文本吗?

Cheers leddy 欢呼声

Edit: 编辑:

I'm using php and when the drop down box is selected, I am querying a mysql db (in the reports.php page) - depending on what the result of that is, decides whether i update the textbox or not: 我使用的是php,当选择了下拉框时,我正在查询mysql db(在reports.php页面中)-根据结果是什么,决定是否更新文本框:

function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}   

// Change the value of the outputText field
function setOutput(){
    if(httpObject.readyState == 4){
        document.getElementById('outputText').value = httpObject.responseText;
    }

}


function checkException()
{

    httpObject = getHTTPObject();
    if (httpObject != null)
    {   
        httpObject.open("GET", "reports.php?exceptions="
                        +document.getElementById('exceptionsID').value+"&date1=" + document.getElementById('date1').value, true);
        httpObject.send(null);
        httpObject.onreadystatechange = setOutput;
    }
}

var httpObject = null;

the textbox is 'outputText' and is set in the setOutput() function 文本框为“ outputText”,并在setOutput()函数中设置

Hope this explains a little better 希望这可以解释得更好

Cheers 干杯

Something like this should change the text of the label. 这样的事情应该更改标签的文本。

// Change the value of the outputText field
function setOutput(){
        if(httpObject.readyState == 4){
                document.getElementById('outputText').innerHTML= httpObject.responseText;
        }

}

I updated my original post to reflect the code that was provided. 我更新了原始帖子以反映所提供的代码。 This assumes that you are using the <label> , <div> , or <span> tags 假设您正在使用<label><div><span>标记

Change the textbox into a span, and keep the id the same. 将文本框更改为一个跨度,并保持ID不变。 Then change the code to set the innerHTML instead of the value: 然后更改代码以设置innerHTML而不是值:

// Change the value of the outputText field
function setOutput(){
        if(httpObject.readyState == 4){
                document.getElementById('outputText').innerHTML= httpObject.responseText;
        }

}

Assuming you're using ASP.NET, yes you can. 假设您正在使用ASP.NET,可以。 Using the Text property of the label. 使用标签的Text属性。

The simplest way to handle this without a textbox is to create a SPAN tag, give it an id of "outputText", and then change the innerText of the SPAN like this: 在没有文本框的情况下处理此问题的最简单方法是创建一个SPAN标记,为其指定一个id为“ outputText”,然后按如下所示更改SPAN的innerText:

function setOutput() {
   if(httpObject.readyState == 4) {
       document.getElementById('outputText').innerText = httpObject.responseText;
   }
}

Inside your HTML, you'll have your label defined like this: 在HTML内,您将具有如下定义的标签:

<span id="outputText">Some default text here</span>

Of course, some CSS styling of the SPAN might be a good thing, but that's another matter for another time. 当然,SPAN的某些CSS样式可能是一件好事,但这又是另一回事了。 Depending on your layout needs, a DIV tag may be better than a SPAN. 根据您的布局需要,DIV标签可能比SPAN更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM