2 u010862729 u010862729 于 2016.04.11 18:25 提问

JavaScript onchange运行一次失效

载入时运行了一次,之后失效了,代码如下

add.jsp

 <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/x-icon" href="../favicon.ico" />
<title>添加</title>
<link href="/IMS/css/css.css" rel="stylesheet" type="text/css">
 <script src="/IMS/lib/MakeRequest.js"></script>
<script>
window.onload = initForm;

function initForm() {
     makeRequest(document.getElementById("aname"));
}
</script>
</head>
<body>
    <form action="Department111" method="post" name="add" id="add">
    <input type="hidden" name="action" value="add">
            <table>
                <tr>
                    <td width="120" align="right">名称:</td>
                    <td width="180" align="left"><input name="aname" type="text" id="aname" autofocus="autofocus" date="Department?action=s&name=" class="reqd"></td>
                </tr>
                <tr>
                    <td align="right">权限:</td>
                    <td align="left"><input name="level" id="level" class="reqd"></td>
                </tr>
                <tr>
                    <td align="right">
                            <a href="index.jsp">返回</a>
                        </td>
                    <td align="left"><input name="add" type="submit" id="add" value="添加"></td>
                </tr>
            </table>
    </form>
</body>
</html>

MakeRequest.js

function makeRequest(obj) {
    obj.addEventListener("onchange", makeR(obj), false)
    function makeR(obj) {
        console.log(obj);
        var url = obj.getAttribute("date") + obj.value;
        console.log(obj.value);
        console.log(url);
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            if (window.ActiveXObject) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    console.log(e);
                }
            }
        }

        if (xhr) {
            xhr.addEventListener("readystatechange", showContents, false);
            xhr.open("GET", url, true);
            xhr.send(null);
        } else {
            msg.innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
        }
        function showContents() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var outMsg = xhr.responseXML.getElementsByTagName("s")[0].childNodes[0].nodeValue;
                    if (outMsg == "yes") {
                        alert("已存在");
                    }
                } else {
                    var outMsg = "There was a problem with the request "
                            + xhr.status;
                    alert(outMsg);
                }
            }

        }
    }

}

3个回答

showbo
showbo   Ds   Rxr 2016.04.12 09:30
已采纳

obj.addEventListener("onchange", makeR(obj), false)
==>
obj.addEventListener("change", function(){makeR(obj)}, false)

u010862729
u010862729 问题已解决,能就是一下吗
一年多之前 回复
hsd2012
hsd2012   2016.04.11 19:14

将 obj.addEventListener("onchange", makeR(obj), false)改为
obj.addEventListener("onchange",function(){makeR(obj);}, false)

u010862729
u010862729   2016.04.18 20:08

最终版本

 function makeRequest(obj) {

    obj.addEventListener("blur", function() {
    doit(obj)
    }, false);

    function doit(obj) {
    if (obj.value != null) {
        var url = obj.getAttribute("date") + obj.value;
        var xhr;
        if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
        } else {
        if (window.ActiveXObject) {
            try {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            console.log(e);
            }
        }
        }
        if (xhr) {
        xhr.addEventListener("readystatechange", showContents, false);
        xhr.open("POST", url, true);
        xhr.send(null);
        } else {
        msg.innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
        }

    }
    function showContents() {
        if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = xhr.responseXML.getElementsByTagName("s")[0].childNodes[0].nodeValue;
            if (outMsg == "1") {
            alert("【" + obj.value + "】已存在");
            obj.focus();
            obj.select();
            }
        } else {
            var outMsg = "There was a problem with the request "
                + xhr.status;
            alert(outMsg);
        }
        }
    }
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!