在动态元素(AJAX)上执行JavaScript函数

时间:2010-06-08 04:51:56

标签: javascript jquery ajax livequery

您好我有以下功能:

... var model = $("#carModel");
.... model.change(validModel);

function validModel(){
     if(model.val() == 0){
            model.addClass("errorJS");
            return false;
            }else{
            model.removeClass("errorJS");
            return true;
            }  
    }

我从AJAX调用后生成的选择框中获取carmodel id。我可以在Firebug控制台中获取它的值,但函数doest不会执行。即使很难我使用model.livequery(validModel);

//如果函数返回false,errorJS类会在元素周围放置一个红色边框

3 个答案:

答案 0 :(得分:1)

在其他问题中,model在函数上下文中没有任何意义。此外,使用此函数调用change(validModel)是没有意义的,因为它只返回true / false值。您需要执行以下操作:

model.change(function() {
    var truefalse = validModel(this);
    //do something with truefalse
});

并将validModel的定义更改为

var validModel = function(elem){
   if($(elem).val() == undefined){
       $(elem).addClass("errorJS");
       return false;
   } else {
       $(elem).removeClass("errorJS");
        return true;
   } 
}

答案 1 :(得分:0)

function getXMLHTTP() {
    var xmlhttp=false;  
    try{
    xmlhttp=new XMLHttpRequest();
    }
    catch(e){       
    try{            
    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e1){
    xmlhttp=false;
      }
     }
  }
   return xmlhttp;
}

function getModel(idCar) {      
    var strURL="http://localhost/root509dir/Includes/model.php?idCar="+idCar;
    var req = getXMLHTTP();
    if (req) {
          req.onreadystatechange = function() {
          if (req.readyState == 4) {
          if (req.status == 200) {document.getElementById('modelCont').innerHTML=req.responseText;
     } else {
       alert("There was a problem while using XMLHTTP:\n" + req.statusText);
       }
    }               
}           
    req.open("GET", strURL, true);
    req.send(null);
    }       
}

model.php

if(isset($_GET['idCar'])){
    $idCar = mysql_real_escape_string($_GET['idCar']);
}
$modelQR = mysql_query("SELECT *FROM model WHERE from_make = '$idCar'") or die(mysql_error());?>
<?php echo htmlentities(PUB_AN_MOD); ?><em>*</em>
<select name="carmodel" id="carmodel">
<option value="0">----------------</option>
<?php while($model = mysql_fetch_array($modelQR)){?>
<option value="<?php echo htmlentities($model['id_model']);?>">
          <?php echo htmlentities($model['car_model']);?></option>
<?php }?>
</select>

cars.php

<?php
$QRcars = mysql_query("SELECT *FROM car ORDER BY car_make") or die(mysql_error());
if(mysql_num_rows($QRcars)>0){?>
<select name="cars" id="cars" onchange="getModel(this.value);"><option value="0">-------------------</option>
<?php while($cars = mysql_fetch_array($QRcars)){?>
<option value="<?php echo htmlentities($cars['id_car']);?>">
<?php echo htmlentities($cars['car_make']);?></option>
<?php }?>
</select>       
<?php }?>
<span id="modelCont">
<?php echo htmlentities(PUB_AN_MOD); ?><em>*</em>
<select name="model" id="model">
<option value="0">---------</option>
</select>
</span>

答案 2 :(得分:0)