JQuery Ajax函数在更多地方

时间:2014-06-30 07:29:08

标签: jquery ajax

我有一个包含这些元素的html页面

<DIV ID="PI"><div id="selection1"  style="display: table-row;">
                PI CODE:<input id="ecode" type="text" name="ecode" onblur="loadEname(this.value);" value="<?php echo $code; ?>">
                PI Name:<input id="ename" type="text" name="ename" readonly>
            </div>
        </DIV>
       <DIV ID="CO_PI"><div id="selection2"  style="display: table-row;">
                CO PI CODE:<input id="ecode" type="text" name="ecode" onblur="loadEname(this.value);" >
                CO PI Name:<input id="ename" type="text" name="ename" readonly>
            </div>
        </DIV>

和JQuery Ajax函数

function loadEname(str) {
    $.ajax({
        url: '../PhpProject1/UserNameCheck.php',
        dataType: 'json',
        type: 'POST',
        data: {"ecode": str},
        //data: "ecode="+ str,
        success: function(response) {
            //alert(response.ename);
            $('#ename').val(response.ename);
        },
        error: function(x, e) {
            if (x.status == 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status == 404) {
                alert('Requested URL not found.');
            } else if (x.status == 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e == 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e == 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }
    });
}

和一个从数据库获取结果的php页面。结果是json.key的形式,带有ename和value以及来自数据库的相应值。 但是在我的html页面中,select = div的id = ename显示了结果,但是select = ename的select2将不会得到ename id的结果,但它会转到selection1.how来解决这个问题?提前谢谢你

1 个答案:

答案 0 :(得分:0)

ID应该是唯一的,因此请从输入中删除id=ename,然后执行以下操作:

....
<input id="ecode" type="text" name="ecode" onblur="loadEname(this);" value="<?php echo $code; ?>">
...

js part

function loadEname(obj) {
    var str = obj.value,
        curEle = obj;
    $.ajax({
        url: '../PhpProject1/UserNameCheck.php',
        dataType: 'json',
        type: 'POST',
        data: {"ecode": str},
        //data: "ecode="+ str,
        success: function(response) {
            //alert(response.ename);
            $(curEle).next('input[name="ename"]').val(response.ename);
        },
      ....