2015-07-28 3 views
0

У меня есть JavaScript для отображения и скрытия различных частей моей страницы:Javascript не удается периодически, но работает, когда страница обновляется

function showCustomer(str) { 
    // remove white space from the name entered on the search screen 
    str = str.replace(" ", "%20"); 
    var xmlhttp; 

    if (str == "") { 
     document.getElementById("txtHint").innerHTML = "No records found"; 
     return; 
    } 

    if (window.XMLHttpRequest) { 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     // code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      document.getElementById("txtHint").innerHTML = xmlhttp.responseText; 
     } 
    } 

    xmlhttp.open("GET", "get_customer.asp?q=" + str, true); 
    xmlhttp.send(); 
} 

function enable_submit() { 
    document.getElementById("id_simplesearch_submit").style.visibility = "visible"; 
    document.getElementById("autocomplete-search").reset(); 
    document.getElementById("txtHint").style.display = "none"; 
    document.getElementById("results").style.visibility = "visible"; 
} 
function disable_submit() { 
    document.getElementById("id_simplesearch_submit").style.visibility = "hidden"; 
    document.getElementById("id_simplesearch").reset(); 
    document.getElementById("txtHint").style.visibility = "visible"; 
    document.getElementById("results").style.display = "none"; 
} 

У меня есть текстовое поле с

onkeyup="showCustomer(this.value)" 

и

onfocus="disable_submit()" 

и форма, определяемая как

<div class="commandspace"> 
    <form id="id_simplesearch" name="simplesearch" method="post" action="index.asp"> 
    <div class="simplesearch"><label for="forename">Forename</label><input type="text" id="id_forename" name="forename" onfocus="enable_submit()" /></div> 

    <div class="simplesearch"><label for="surname">Surname</label><input type="text" id="id_surname" name="surname" onfocus="enable_submit()" /></div> 
    <div class="simplesearch"><label for="unit">Unit/Team</label><input type="text" id="id_unit" name="unit" onfocus="enable_submit()" /></div> 

    <div id="simplesearchsubmit"> 
    <div class="simplesearch"> 
    <input class="simplesearchsubmit" type="submit" id="id_simplesearch_submit" name="search" value="Search" /> 

При первой загрузке страницы и начале ввода в окне поиска ShowCustomer элемент txtHint отображает и начинает извлекать данные обратно. Если, однако, я щелкнул одну из трех ящиков simplesearch, а затем вернусь обратно в окно поиска showCustomer, txtHint не отобразится вообще.

+0

Мне сложно определить, в чем проблема. «ShowCustomer» не работает после того, как ввод размыт? Или 'txtHint' не отображается должным образом. Не могли бы вы отредактировать свой пост, так что он станет немного яснее? – Mouser

+0

Разве это понятно? – wrichards0

+0

Да, проблема решена. – Mouser

ответ

1

Проблема здесь (см комментарии для ответа):

function enable_submit() { 
    document.getElementById("id_simplesearch_submit").style.visibility = "visible"; 
    document.getElementById("autocomplete-search").reset(); 
    document.getElementById("txtHint").style.display = "none"; // <----- This uses display 
    document.getElementById("results").style.visibility = "visible"; 
} 
function disable_submit() { 
    document.getElementById("id_simplesearch_submit").style.visibility = "hidden"; 
    document.getElementById("id_simplesearch").reset(); 
    document.getElementById("txtHint").style.visibility = "visible"; // <----- This uses visibility 
    document.getElementById("txtHint").style.display = "block"; // <----- This line should fix your problems. 
    document.getElementById("results").style.display = "none"; 
} 

Так enable_submit скрывает элемент с помощью дисплея. Пока disable_submit пытается показать это, используя видимость. Два свойства стиля несовместимы.

Смежные вопросы