我想在特定情况下加载html。例如,如果ID和密码正确,我们可以在网站上看到的登录按钮仅加载页面(html)。
<a class="btn btn-primary btn-lg" id="singIn" href="#" role="button">Sign In</a>
这是html中的登录按钮。
$('#singIn').click(function() {
var userID = $('#userID').val();
var userPW = $('#userPW').val();
var tempID = "test";
var tempPW = "12345";
if (userID === tempID && userPW === tempPW) {
alert("userID : " + userID + ", userPW : " + userPW);
//TODO : load the local html file.
//var url = $(this).attr("href");
} else {
alert("userID : " + userID + ", userPW : " + userPW);
//TODO : load the other local html file or just alert.
}
});
这是我想要加载我的本地html文件的部分(if / else语句),正如我先提到的那样。
我怎样才能做到这一点?请告诉我。谢谢。
尝试这样的事情,
$(this).attr('href', 'local url goes here');
尝试使用隐藏按钮进行实际提交的验证功能:
$('[type="button"]').click(function() {
var valid = true;
$.each($('input'), function() {
valid = valid && $(this).val().length > 0;
})
if (valid) {
var location = 'http://www.rootdir.com/?user=' + $('input').get(0).value;
location += '&password=' + $('input').get(1).value;
alert(location);
// window.location.href = location; <-- uncomment to redirect to this url
} else {
alert('Please fill out all fields!');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="Enter Name" />
<input type="password" placeholder="Enter Password" />
<button type="button">Submit</button>
<button type="submit" id="realSubmit" style="display:none"></button>