dsgawmla208057 2010-10-07 06:48
浏览 50

JQuery Validation无法处理Ajax控件......!

Hi there I have Problem with my Code I just want to validate States which is generated from Ajax response.text

Here is the JQuery for State Field:

$(document).ready(function () {
    var form = $("#addStudentfrm");

    var state = $("#state");
    var stateInfo = $("#stateInfo");

    state.blur(validateStates);
    state.keyup(validateStates);


    function validateStates() {
        if (state.val() == '') {
            state.addClass("error");
            stateInfo.text("Please Select/Enter State");
            stateInfo.addClass("error5");
            return false;
        } else {
            state.removeClass("error");
            stateInfo.text("");
            stateInfo.removeClass("error5");
            return true;
        }

    }
});

Here PHP Function for Get All States in Respected Country:

public function getAllCountryStates($post){
$que = "Select * from ".WMX_COUNTRY." where code = '".$post[value1]."'";
$cRes = $this->executeQry($que);
$cResult = $this->getResultRow($cRes);
$cId = $cResult['id'];
$stateObj = new Admin;
$rdat = $post['opr'];
$rdtar = explode('.', $rdat);
$res = @mysql_fetch_row(mysql_query("Select * from ".$rdtar['1']." where id = ".$rdtar['0']));
$usts = $res['state'];
$result = $stateObj->selectQry(WMX_STATE,"country_id=$cId",'','');
$number = $stateObj->getTotalRow($result);

if($number > 0){
        $getSelect ="<select name='state' id='state' class='textboxcss'>";
    while($stateVal = $stateObj->getResultRow($result)){
               $getSelect.="<option value='".$stateVal[state]."'>$stateVal[state]</option>";
}
    $getSelect.="</select>";
}else{
    if($usts!=''){
        $getSelect = "<input type='text' name='state' id='state'class='textboxcss' value='$admnState'/>";
    } else {
        $getSelect = "<input type='text' name='state' id='state' class='textboxcss'/>";
    }
}
    echo $getSelect;  }

In the Initial State Text box getting validate

but when the control comes with the Ajax Response Jquery wont validate it for Blank Entries

my Ajax Function:

function DataByPost(url,objId,postData,div_id){
    var selId = objId.split('|');
    var passData = postData;
    var AJAX = null;
    if (window.XMLHttpRequest) {
       AJAX=new XMLHttpRequest();
    } else {
       AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }
   if (AJAX==null) {
   alert("Your browser doesn't supportAJAX.");                      
       return false
   } else {
           AJAX.open("POST",url,true);
       AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       AJAX.onreadystatechange = function() {
            if (AJAX.readyState==4 || AJAX.readyState=="complete"){  
                alert(AJAX.responseText);
                var msg=AJAX.responseText;
                var idary = new Array();
                document.getElementById(selId).value = msg;
                document.getElementById(selId).innerHTML = msg;
            }
       }
       AJAX.send(passData);
   }
}
//First Function 
function showContent(url,arg,opr,sel_id,div_id)
{   
 var postData='';   
 var formVal=arg.split('|');
 if(document.getElementById(div_id))   
           document.getElementById(div_id).style.display='';
 if(document.getElementById(sel_id))   
 document.getElementById(sel_id).style.display='';
    for(i=1;i<=formVal.length;i++) 
        var postData =postData + 'value'+i+'='+escape(formVal[i-1])+'&';
        postData=postData + 'opr='+opr;
    DataByPost(url,sel_id,postData,div_id);         
}
  • 写回答

1条回答 默认 最新

  • duanmo5724 2010-10-07 07:06
    关注

    I don't know exactly what is going on here without seeing the actual page, but a few things you might try:

    1. Use jQuery to do your ajax since you're already using it. It is bug-free and cross platform, and that way you can be sure there are no bugs in the ajax part.

    2. I would move the lines "var state = $("#state"); var stateInfo = $("#stateInfo");" inside of the function then declare the function outside of your document.ready block. That way you can be sure that every time the function gets called, it has access to the variables.

    3. If your ajax call is replacing the input you're validating, you'll need to re-bind the events each time your ajax call finishes. With jQuery you can do this using the callback parameter.

    I'm assuming number 3 is your problem, so try it first.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?