似乎下面发布的代码中的第二个警告不会出现; alert("The user is succesfully been added!");
不会显示。我的每个文本框都有一个脚本,用户名,名字,姓氏和密码。
首先,我尝试为save btn和textbox单独发出警报;场景是当我单击带有空字段的保存按钮时,它将显示添加用户成功的警报。然后它将显示我需要填写文本框的另一个警报。所以我在想,如果我检查空字段,成功警报就不会显示,只会在我输入正确的内容时显示。但发生的事情是,它不会显示我期望显示的警报。
整个添加用户表单的代码:
<div id="add_user" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-body">
<div class="alert alert-info"><strong>Add User</strong></div>
<form class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label" for="inputEmail">Username</label>
<div class="controls">
<input type="text" id="inputEmail" name="username" placeholder="Username" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" name="password" id="inputPassword" placeholder="Password" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Firstname</label>
<div class="controls">
<input type="text" id="inputEmail" name="firstname" placeholder="Firstname" pattern="[A-Za-z].{1,}"
required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Lastname</label>
<div class="controls">
<input type="text" id="inputEmail" name="lastname" placeholder="Lastname" pattern="[A-Za-z].{1,}"
required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button name="submit" onClick="return saveuservalidation()" class="btn btn-success"><i
class="icon-save icon-large"></i> Save
</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close
</button>
</div>
</div>
<script>
function saveuservalidation() {
if (firstname == "" || lastname == "" || username == "" || password == "") {
return false;
} else {
return true;
alert("The user is succesfully been added!");
}
}
</script>
<?php
if (isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
mysql_query("insert into users (username,password,firstname,lastname) values('$username','$password','$firstname','$lastname')")or die(mysql_error());
}
?>
javascript :(这是正确的方式吗?)
<script>
function saveuservalidation() {
if (firstname == "" || lastname == "" || username == "" || password == "") {
return false;
} else {
return true;
alert("The user is succesfully been added!");
}
}
</script>
OR
<script>
function saveuservalidation() {
if (firstname == "" || lastname == "" || username == "" || password == "") {
return false;
} else {
alert("The user is succesfully been added!");
return true;
}
}
</script>
它不会显示任何警报。
答案 0 :(得分:4)
只要你return
,函数就会返回,之后的所有内容都不会被执行(无法访问)。将其更改为
else{
alert("The user is succesfully been added!");
return true;
}
并显示警告。