1

我试图添加一个异常,例如如果用户不输入任何内容或输入错误的“联系人 ID”并按 Enter,则会出现错误消息并通知用户未找到 ID。有什么帮助吗?在此先感谢:) 这是我的 index.html 文件。

<html> 
    <body> 
        <script language="javascript" type="text/javascript">

    function ajaxFunction(e){
    var e=e || window.event;
    var keycode=e.which || e.keyCode;
    if(keycode==13 || (e.target||e.srcElement).value==''){ 
    var http;  // The variable that makes Ajax possible! 

    try{ 
        // Opera 8.0+, Firefox, Safari 
        http = new XMLHttpRequest(); 
    } catch (e){ 
        // Internet Explorer Browsers 
        try{ 
            http = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try{ 
                http = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e){ 
                // Something went wrong 
                alert("Your browser broke!"); 
                return false; 
            } 
        }
    }

    var url = "getagentids.php?param=";
                var idValue = document.getElementById("agid").value;
                var myRandom = parseInt(Math.random()*99999999);  // cache buster
                http.open("GET", "getagentids.php?param=" + escape(idValue) + "&rand=" + myRandom, true);
                http.onreadystatechange = handleHttpResponse;
                http.send(null);
         function handleHttpResponse() {
                    if (http.readyState == 4) {
                        results = http.responseText.split(",");
                        document.getElementById('agfn').value = results[0];
                        document.getElementById('agsal').value = results[1];
                        document.getElementById('agtel').value = results[2];
                        document.getElementById('agid').value = results[3];
                    }
                } 
    }
}

</script> 

       <form>
            <table>
                <tr>
                    <td>Contact ID:</td>
                    <td><input id="agid" type="text"
                               name="contactid" onkeyup="ajaxFunction(event)"></td>
                </tr>
                <tr>
                    <td>Tel Number:</td>
                    <td><input id="agtel" type="text"
                               name="contacttel"></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td><input id="agfn" type="text"
                               name="contactfullname"></td>
                </tr>
                <tr>
                    <td>Salutation:</td>
                    <td><input id="agsal" type="text"
                               name="contactsalutation"></td>
                </tr>
                <tr>
                    <td><input type="reset" value="Clear"></td>
                    <td></td>
                </tr>
            </table>
        </form>

    </body> 
</html> 
4

2 回答 2

2

以下是我将如何使用jQuery进行基本验证。

$(document).ready(function(){
    $('#contact').keypress(function(e){ //when user presses a key in that Input Box
      var code = (e.keyCode ? e.keyCode : e.which);
      if(code == 13) // check if it is Enter Key
      {
        if($(this).val().trim().length == 0) // check if it is empty
            alert("Error: Shouldn't be empty !");
        else
        {
            // You are good to go here
        }
      }
    });
});​

HTML:

<input id="contact" name="contact" value="" />​

如果您想现场测试,请转到:http: //jsfiddle.net/NFSTL/

我将KeyPress事件绑定到该 InputBox。

希望能帮助到你。:)


编辑

不使用 jQuery:

function ajaxFunction(e){
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    if(code==13)
    {
        if(targ.value.trim().length == 0)
            alert("Error: This shouldn't be empty!");
        else
        {
            //do the sending and receiving of data here...
        }
    }
}​

HTML:

<input id="agid" type="text" name="contactid" onkeyup="ajaxFunction(event)" />​

对于现场测试:http: //jsfiddle.net/SYf4C/

这是我使用的一个很好的教程:quirksmode.org/js/events_properties

我会说,jQuery 让生活更轻松。:) 因为它是一个 JavaScript 库,几乎可以在所有浏览器中运行。

于 2012-11-24T07:27:10.910 回答
1

主页编码:

 $('.vote').click(function() {
        var valueid=$(this).attr("alt");
        $.ajax({
            type: "POST",
            url: "voteajax.php",
            data: "votebtn="+valueid+""
        }).done(function( msg ) {
            var received_data=msg.trim();
            if(received_data=='0')
            {
                alert('Please sign in to vote!');
            }
            else
            {
                alert('received data'+received_data);
                gg2=received_data.split("/");
                var x=gg2[0];
                var no_of_vote=gg2[1];
                $('#totalnoofvotespan').html(no_of_vote);
                window.location='<?php echo $_SERVER['REQUEST_URI']; ?>';
            }
        });


//Ajax Page Coding


    $poll_choice = $_POST['votebtn'];
    $userid=$_SESSION['userid'];
    $date=date('Y-m-d h:i:s');
    $ex=explode(",",$poll_choice);
    $pollid=$ex[0];
    $choiceid=$ex[1];
    if($userid=="")
    {   
        echo "0";   
    }
    else
    {
        ..// Your coding 
        echo $choiceid."/".$numofvote;   // Echo the identifier and access this identifier in main page for message notification
    }

第一个是主页面编码 在上面的编码中,我将参数传递给 ajax 页面。在 ajax 页面中,它从主页获取值,如果它无效,它会回显标识代码。使用该代码,我们可以说“它不是有效的”。像这样你必须尝试。我认为这可能会帮助你

于 2012-11-24T07:04:48.627 回答