Javascript& A的问题AJAX - 选择表单的事件监听器不适用于更改?

时间:2013-01-06 01:15:03

标签: javascript ajax forms xmlhttprequest addeventlistener

我是javascript的新手,我正在构建一个基本网站,有人可以搜索汽车的小型数据库,在用户选择搜索条件时更新搜索结果。

我试图通过在我的html中创建一个表单,多个选择(CODE 1),javascript与事件监听器进行选择,ajax通过PHP文件搜索数据库(CODE 2)。< / p>

代码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'变量,但它没有在“制作”选择框中进行新选择时更改。

出于某种原因,我的代码现在根本不起作用,甚至不会显示“启动作品”

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

1 个答案:

答案 0 :(得分:0)

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

make.addEventListener('change', function(){
    setmake(this.value)
}, false);