1

我对javascript相当陌生,我正在构建一个基本网站,有人可以通过一个小型汽车数据库搜索该网站,当用户选择搜索条件时,它会更新搜索结果。

我试图通过在我的 html 中创建一个带有多个选择(CODE 1)的表单,以及带有用于选择的事件侦听器的 javascript 和通过 PHP 文件搜索数据库(CODE 2)的 ajax 来做到这一点。

代码 1 - HTML

<div class="intro">
        <p>Enter your search criteria:</p>

            <div id="formbox">
                <form>
                    <!-- MAKE SELECTION -->
                    <select name="make" id="selectmake">
                    <option value="test">Car Make:</option>
                    <option value="Aston Martin">Aston Martin</option>
                    <option value="Ford">Ford</option>
                    <option value="Lotus">Lotus</option>
                    <option value="Vauxhall">Vauxhall</option>
                    </select>&emsp;

                    <!-- MODEL SELECTION -->
                    <select name="model" id="selectmodel">
                    <option value="">Car Model:</option>
                    <option value="DB9 Coupe">DB9 Coupe</option>
                    <option value="Vanquish">Vanquish</option>
                    <option value="Fiesta Hatchback">Fiesta Hatchback</option>
                    <option value="Focus Hatchback">Focus Hatchback</option>
                    <option value="Mondeo Estate">Mondeo Estate</option>
                    <option value="Elise">Elise</option>
                    <option value="Exige">Exige</option>
                    <option value="Astra Hatchback">Astra Hatchback</option>
                    <option value="Corsa D">Corsa D</option>
                    <option value="Frontera 4x4">Frontera 4x4</option>
                    </select>&emsp;

                    <!-- YEAR SELECTION -->
                    <select name="year" id="selectyear">
                    <option value="">Car Year:</option>
                    <option value="1998">1998</option>
                    <option value="2000">2000</option>
                    <option value="2001">2001</option>
                    <option value="2004">2004</option>
                    <option value="2007">2007</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    </select>&emsp;

                    <!-- CC SELECTION -->
                    <select name="cc" id="selectcc">
                    <option value="">Car CC:</option>
                    <option value="1250">1250</option>
                    <option value="1400">1400</option>
                    <option value="1600">1600</option>
                    <option value="1800">1800</option>
                    <option value="2000">2000</option>
                    <option value="3200">3200</option>
                    <option value="5900">5900</option>
                    <option value="6000">6000</option>
                    </select>&emsp;

                    <!-- COLOUR SELECTION -->
                    <select name="colour" id="selectcolour">
                    <option value="">Car Colour:</option>
                    <option value="Black">Black</option>
                    <option value="Blue">Blue</option>
                    <option value="Red">Red</option>
                    <option value="Silver">Silver</option>
                    </select>&emsp;

                    <!-- RESET SELECTIONS -->
                    <input type="reset" />
                </form>
            </div>
        </div>

        <!-- SEARCH RESULTS -->
        <div id="results">
            Your results will be displayed here
        </div>

代码 2 - JAVASCRIPT 文件

function initialise() {
//assign selection fields to variables
var i=0;
var make=document.getElementById("selectmake");
make.addEventListener('change', setmake(this.value), false);
var model=document.getElementById("selectmodel");
model.addEventListener('change', setmodel(this.value), false);
var year=document.getElementById("selectyear");
year.addEventListener('change', setyear(this.value), false);
var cc=document.getElementById("selectcc");
cc.addEventListener('change', setcc(this.value), false);
var colour=document.getElementById("selectcolour");
colour.addEventListener('change', setcolour(this.value), false);

//assign results area to a variable
var results=document.getElementById("results");

//setting search criteria to return all results by default
var makesearch="make=make";
var modelsearch="model=model";
var yearsearch="year=year";
var ccsearch="cc=cc";
var coloursearch="colour=colour";
results.innerHTML="initiate works";

//function to set the search to search make if set to an existent value
function setmake (makevar) {
    i=i+1;
    results.innerHTML=i;
    if (makevar.length>0) {
        makesearch="make='"+makevar+"'";
        results.innerHTML="EVENTIFFUNCTIONworks";
        constructsearch();
    }
}

function constructsearch() {
    var searchstring="WHERE "+makesearch+" AND "+modelsearch+" AND "+yearsearch+" AND "+ccsearch+" AND "+coloursearch;
    results.innerHTML="construct works";
    sendsearch(searchstring);
}
}

//function to update search results
function sendsearch(searchstring) {
//update search results with an XMLHttpRequest
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("results").innerHTML="XMLworks";
    //xmlhttp.responseText;
  }
}

xmlhttp.open("GET","results.php?search="+searchstring,true);
xmlhttp.send();
}

window.onload=initialise;

变量 'i' 已创建,它的所有用途都是用于测试......我原来的工作是这样的,当页面加载时,结果 div 将显示 'i' 变量,但是当在“制作”选择框中进行了新的选择。

由于某种原因,我的代码现在根本无法工作,甚至不会显示“启动工作”

任何帮助都非常感谢,并提前感谢。

4

1 回答 1

0

addEventListener将函数作为第二个参数,因为您没有传递函数,而是调用函数。您可以使用匿名函数表达式来包装您在调用addEventListener. 对于你们中的每个人,请addEventListener执行以下操作

make.addEventListener('change', function(){
    setmake(this.value)
}, false);
于 2013-01-06T01:38:02.567 回答