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

为什么我写的js代码没有起作用
本帖最后由 cherry_chenxiao 于 2014-05-09 18:00:50 编辑
<form action="" method="POST" class="form1" onsubmit="return do_submit();">
<table class="form1">
<tr><td><label>用户名:</label><input type="text" name="reg[username]" value="<?php echo $data['username'];?>" id="username" class="input-text1"/></td></tr>
<tr><td><label>密码:</label><input type="password" name="reg[pwd]" value="<?php echo $data['pwd'];?>" id="pwd" class="input-text1"/></td></tr>
<tr><td><label>确认密码:</label><input type="password" name="reg[repwd]" value="<?php echo $data['repwd'];?>" id="repwd" class="input-text1"/></td></tr>
<tr><td><label>邮箱:</label><input type="text" name="reg[email]" value="<?=$data['email']?>" id="email" class="input-text1"/></td></tr>
<tr><td><label>性别:</label><input type="radio" name="reg[sex]" />男
     <input type="radio" name="reg[sex]" />女</td></tr>
<tr><td><label>QQ:</label><input type="text" name="reg[QQ]" value="<?php echo $data['QQ']?>" id="QQ" class="input-text1"/></td></tr>
<tr><td><label>个人主页:</label><input type="text" name="reg[homepage]" value="<?php echo $data['homepage'];?>" id="homepage" class="input-text1"/></td></tr>
<tr><td><label>生日:</label><input type="text" name="reg[birth]" value="<?php echo $data['birth'];?>" id="birth" class="input-text1"/></td></tr>
<tr><td><label>现住城市:</label><input type="text" name="reg[city]" value="<?php echo $data['city'];?>" id="city" class="input-text1"/></td></tr>
<tr><td><label>家庭住址:</label><input type="text" name="reg[addr]" value="<?php echo $data['addr'];?>" id="adr" class="input-text1"/></td></tr>
<tr><td>
<div class="input-code">
<input type="hidden" name="act" value="register"/>
<input type="submit" value="立即注册"  />
<input type="reset"  value="重新填写" />
</div>
</td></tr>
</table>  
</form>
/*-------省略部分代码---------*/
<script language="javascript">
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g, '');}
function do_submit() {
var username = document.getElementById('username');
if ( username.value.trim() == '' ) {
window.alert('用户名尚未填写。');
username.className = 'input-error'; //设置CSS样式
username.focus(); //聚焦
username.value = '';
return false; //阻止表单提交
}

var pwd = document.getElementById('pwd');
if ( pwd.value.trim() == '' ) {
window.alert('密码尚未填写。');
pwd.className = 'input-error';
pwd.focus();
pwd.value = '';