日期:2014-05-16  浏览次数:20947 次

jquery nodename为空或不是对象 - Web 开发 / Ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
body,td,th {
font-family: "Helvetica Neue", Helvetica, Arial, Tahoma, Verdana, sans-serif;
}
body {
background-color: #CC0;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" charset="utf-8" href="register.css" />
<script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
  
  $(document).ready(function() {
$("#register, #login").click(function(e) {
var name = ($(event.target).attr('id') == 'register') ? 'Registration' : 'Login';
$('#message').slideUp('fast');

$.post('service.php', $('#mainform').serialize() +'&action='+ $(event.target).attr('id'), function(data) {
var code = $(data)[0].nodeName.toLowerCase();

$('#message').removeClass('error');
$('#message').removeClass('success');
$('#message').addClass(code);
if(code == 'success') {
$('#message').html(name + ' was successful.');
}
else if(code == 'error') {

var id = parseInt($(data).attr('id'));
switch(id) {
case 0:
$('#message').html('This username has already been taken. Try some of these suggestions:<br/><br/>');

form = $(document.createElement('form'));
$(data).find('suggestions > suggestion').each(function(idx, el) {
radio = $(document.createElement('input'));
radio.attr({type: 'radio', name: 'suggested', id: 'suggested_'+idx, value: el.innerHTML});

lbl = $(document.createElement('label'));
lbl.attr('for', 'suggested_'+idx);
lbl.html(el.innerHTML);

form.append(radio);
form.append(lbl);
form.append('<br/>');
});
$('#message').append(form);

$('#message form input[type="radio"]').click(function() {
$('#username').val($(this).attr('value'));
});
break;
case 1:
$('#message').html('The email entered is invalid.');
break;
case 2:
$('#message').html('The username or password you entered was invalid.');
break;
default:
$('#message').html('An error occurred, please try again.');
}
}
$('#message').slideDown('fast');
});
return e.preventDefault();
});
});
  </script>
  <title>Registration</title>
</head>
<body>
<div id="container">
<div id="message"></div>
<form method="post" id="mainform">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="" />

<label for="password">Password</label>
<input type="password" name="password" value="" />

<input type="submit" name="action" id="login" value="Log in" />

<h2>Extra options (registration only)</h2>

<label for="firstname">First name</label>
<input type="text