段落

时间:2018-07-03 14:08:06

标签: javascript ajax

我有一个搜索表单,当有人输入薪水(在我的情况下是唯一的)时,它从数据库中返回相应的值。我正在使用ajax来做到这一点,我设法在输入字段中检索值。输入文本上有一个onchange事件,该事件在“ UpdateCityState”中调用

我想显示段落中的值而不是输入字段。

<p id='employee_name'></p>
<p id='employee_age'></p>
<p id='safe_code'></p>

<script>
var ajax = getHTTPObject();

function getHTTPObject()
{
var xmlhttp;
if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} else {
    //alert("Your browser does not support XMLHTTP!");
}
return xmlhttp;
}

function updateCityState()
{
    if (ajax)
    {
        var employee_salary = document.getElementById("employee_salary").value;

        if(employee_salary)
        {

            var param = "?employee_salary=" + employee_salary;
            var url = "test04.php";

            ajax.open("GET", url + param, true);
            ajax.onreadystatechange = handleAjax;
            ajax.send(null);

        }
    }
}
function handleAjax()                                                                                                                           
{
    if (ajax.readyState == 4)
    {
        var employee_name = document.getElementById('employee_name');
        var employee_age = document.getElementById('employee_age');
        var safe_code = document.getElementById('safe_code');
        if(!!ajax.responseText) {
            var result = JSON.parse(ajax.responseText);
            if(!!result){
                employee_name.value  = (!!result.employee_name) ? result.employee_name : '';
                employee_age.value = (!!result.employee_age) ? result.employee_age : '';
                safe_code.value = (!!result.safe_code) ? result.safe_code : '';
            } 
        }
    }
}

我尝试用employee_name.text替换employee_name.value但没有运气

1 个答案:

答案 0 :(得分:0)

如果将value替换为innerHTML,则一切正常。