我试图添加一个例外,例如,如果用户输入任何内容或输入错误的“联系人ID”并按回车键,则会出现错误消息&通知用户未找到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>
答案 0 :(得分: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库,几乎可以在所有浏览器中使用。
答案 1 :(得分: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页面中,它从主页面获取值,如果它无效,则回显标识代码。使用该代码我们可以说“它不是有效的”。像这样你必须尝试。我想这可能会对你有所帮助