我被困在那里 :/.. 我想如果例如#firstname 長度超過 20 說一些我做的事情,如果它是空的,但我不知道如何,如果這里有人知道,我會很高興!
$("#register").click(function(e) {
e.preventDefault();
var sex = 'male';
var firstname = '';
var lastname = '';
var date = '';
if ($('.container[data-sex="female"] input:checked').val() == 'on') {
sex = 'female';
}
if ($('#firstname').val() != '' && $('#firstname').val() != null) {
firstname = $('#firstname').val();
}else {
return Information('Firstname can\'t be empty!');
}
if ($('#lastname').val() != '' && $('#lastname').val() != null) {
lastname = $('#lastname').val();
}else {
return Information('Lastname can\'t be empty!');
}
if ($('#date').val() != '' && $('#date').val() != null) {
date = $('#date').val();
}else {
return Information('Date can\'t be empty!');
}
$('#core').fadeOut(300);
$.post('https://lion-register/CreateCharacter', JSON.stringify({
firstname : firstname,
lastname : lastname,
sex : sex,
date : date,
queue : MX.CurrentCharacter
}));
});
uj5u.com熱心網友回復:
使用純 JavaScript,如下所示:
var value = document.querySelector("#firstname").value;
value.length; // 10
使用 jQuery:
var value = $("#firstname").val();
value.length; // 10
這是一個作業示例:
function Information(msg) {
simple.alert(msg, () => {});
}
$("#register").click(function(e) {
e.preventDefault();
var sex = 'male';
var firstname = '';
var lastname = '';
var date = '';
if ($('.container[data-sex="female"] input:checked').val() == 'on') {
sex = 'female';
}
if ($('#firstname').val() !== '' && $('#firstname').val() != null && $('#firstname').val().length >= 20) {
firstname = $('#firstname').val();
} else {
if ($('#firstname').val().length < 20) {
return Information('Invalid firstname');
} else {
return Information('Firstname can\'t be empty!');
}
}
if ($('#lastname').val() !== '' && $('#lastname').val() != null && $('#lastname').val().length >= 20) {
lastname = $('#lastname').val();
} else {
if ($('#lastname').val().length < 20) {
return Information('Invalid lastname');
} else {
return Information('Lastname can\'t be empty!');
}
}
if ($('#date').val() && (/[0-9]/g).test($('#date').val().replace(/\-/g, ""))) {
date = $('#date').val();
} else {
if ($('#date').val() !== '') {
return Information('Date can\'t be empty!');
} else {
return Information('Please enter a date');
}
}
$(this).fadeOut(300);
simple.alert("Successfully registered!");
$.post('https://lion-register/CreateCharacter', JSON.stringify({
firstname : firstname,
lastname : lastname,
sex : sex,
date : date,
queue : MX.CurrentCharacter
}));
});
::selection{background-color:#333;color:#f9f9f9}input:-webkit-autofill{-webkit-box-shadow:0 0 0px 1000px white inset}body{caret-color:#333;-webkit-tap-highlight-color:transparent;font-family:Arial,Helvetica,sans-serif}*{box-sizing:border-box}input[type=text],input[type=date],input[type=password]{width:100%;padding:15px;margin:5px 0 22px 0;display:inline-block;border:none;background:#f1f1f1}input[type=password]:focus,input[type=text]:focus{background-color:#ddd;outline:0}button{background-color:#333;color:#fff;padding:14px 20px;margin:8px 0;border:none;cursor:pointer;width:100%;opacity:.9;border-radius:2px}button:hover{opacity:1}.cancelbtn{padding:14px 20px;background-color:#f44336}#register{float:center;width:100%;position:relative;left:50%;margin-left:-50%}.container{padding:16px}.modal{display:block;position:fixed;z-index:1;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:transparent;padding-top:50px}.modal-content{background-color:#fefefe;margin:5% auto 15% auto;width:90%}hr{border:1px solid #f1f1f1;margin-bottom:25px}.clearfix::after{content:"";clear:both;display:table}@media screen and (max-width:300px){#register{width:100%}}
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Parking-Master/Simple-Alert@latest/simplealert.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Register</title>
</head>
<body>
<div class="modal">
<form class="modal-content" action="" onsubmit="event.preventDefault();">
<div class="container">
<h1 style="text-align:center;font-style:normal;font-family:'Verdana', Verdana, 'Lato', 'Roboto', 'Open sans', Arial, sans-serif;">Register</h1>
<div style="color:#333;text-align:center;"><a style="text-decoration:none;color:#333;border-bottom:0.006vw solid #333;" href="/help.html">Help</a> • <a style="text-decoration:none;color:#333;border-bottom:0.006vw solid #333;" href="#">Login instead</a></div>
<hr>
<label for="email"><b>First name</b></label>
<input autocorrect="off" spellcheck="false" autocomplete="off" id="firstname" type="text" placeholder="Enter firstname" name="firstname" required>
<label for="email"><b>Last name</b></label>
<input autocorrect="off" spellcheck="false" autocomplete="off" id="lastname" type="text" placeholder="Enter lastname" name="lastname" required>
<label for="psw-repeat"><b>Date</b></label>
<input autocorrect="off" spellcheck="false" autocomplete="off" type="date" placeholder="Enter date" id="date" name="date" required>
<label>
<input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
</label>
<p>By creating an account you agree to our <a href="#" style="color:#333">Terms & Privacy</a>.</p>
<hr>
<div class="clearfix">
<button type="submit" id="register">Register</button>
</div>
</div>
</form>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/351018.html
標籤:javascript
下一篇:只用逗號分割字串不留空格
