我在检查某个值是否不在数组中时遇到了一些麻烦.我试图只允许在文本框中按某些键.
这就是我所拥有的:
var keyCodeArr = [ 8,9,13,17,18,37,39,46 ];
$('#txtSearch').keydown(function(event) {
var code = event.keyCode;
if (!$.inArray(code,keyCodeArr)) {
event.preventDefault();
}
});
Run Code Online (Sandbox Code Playgroud)
它允许文本框中的任何内容.我查看了$ .inArray()的文档,其中显示了一个示例,用于查看值是否在数组中,因此我认为只需在其前面添加NOT即可.
或者是我首先没有正确初始化阵列?
我错过了什么?
我有一个jsFiddle来证明我的问题(并允许你们把我拉直).
我只是检查两个输入文本框的值,并在最高价格低于最低价格时提醒用户,但他们正在向后评估!我有,(maxValue < minValue)...但它评估它,就好像操作符"大于".
我错过了什么?!?
<form id="search" action="search.php" method="post">
<label id="lblPriceMin" for="txtPriceMin">Minimum Price</label>
<input type="text" id="txtPriceMin" name="priceMin"></input>
<br />
<br />
<label id="lblPriceMax" for="txtPriceMax">Maximum Price</label>
<input type="text" id="txtPriceMax" name="priceMax"></input>
<br />
<br />
<input type="reset" id="reset" name="reset" value="Clear Form" />
</form>
Run Code Online (Sandbox Code Playgroud)
这是js,
$('#txtPriceMax').focusout(function() {
var minValue = $('input#txtPriceMin').val();
var maxValue = $('input#txtPriceMax').val();
//alert ('minValue: ' + minValue + ', maxValue: ' + maxValue);
if (maxValue < minValue) {
alert ('The maximum value (' + maxValue + …Run Code Online (Sandbox Code Playgroud) 我在页面上设置了以下表格:
<div id="schedule">
<table class="tableListings">
<tbody>
<tr class="tableDate">
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
<table class="tableListings">
<tbody>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
<table class="tableListings">
<tbody>
<tr class="tableDate">
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
<table class="tableListings">
<tbody>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要设置背景颜色只对<td>了S <table>并没有有一个<tr>带班的"tableDate".
我怎样才能做到这一点?
如果我可以用CSS完全做到这一点会更好,但如果它更容易(或必要)我也可以使用jQuery.
这不像我之前没有做过同样的过程,但我无法弄清楚为什么我的PHP脚本的POST数据是空的.这是我做过/找到的:
我已经验证$ .ajax调用的"data"参数有一个值(submitSearch函数和success参数中的警报显示搜索变量的正确值).
我知道脚本正在"找到" - 浏览器的js控制台中没有找到文件.
我没有收到任何数据库连接错误
我也知道PHP脚本正在运行,因为$ .ajax调用的success参数中的警报在PHP脚本的else子句中显示$ message值.
我在PHP脚本中设置的日志记录没有显示POST数据
如果有人能看一眼,并希望指出我所缺少的东西,我将不胜感激.
以下是所有相关代码:
Javascript(在HTML文件中的脚本标记中)
$('input#btnSubmitSearch').click(function() {
// Clear the text box in case an error was indicated previously
$('input#txtSearch').css({'background-color' : ''});
var search = $('input#txtSearch').val();
if (search == '' || search.length != 5) {
alert ('Not a valid entry!');
$('input#txtSearch').css({'background-color' : '#FDC3C3'});
return false;
}
else {
submitSearch(search);
return false;
}
});
function submitSearch(search) {
alert ('Sending ' + search + ' to the database.');
$.ajax({
type: 'POST',
url: 'scripts/search.php', …Run Code Online (Sandbox Code Playgroud) 我知道这已被问了一千(百万?)次,但对于我的生活,我无法弄清楚为什么我不能让div在我的页面上居中.即使HTML和CSS被剥离到最低限度.
如果有人能指出我在这里缺少什么,我会很感激.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
div#branchSelect { margin: 0 auto; }
</style>
</head>
<body>
<div id="branchSelect">
<h4 class="selectBranch">Select a Branch to View</h4>
<select type="select" id="ddlBranches" class="selectBranch">
<option id="defaultBranchesListItem" value="0">Select a branch...</option>
<option value="1">Atlanta</option>
</select>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是一个jsFiddle.
这是我正在初始化和构建数组的方式:
var newCountyInfo = new Object();
newCountyInfo.name = newCountyName;
newCountyInfo.state = newCountyState;
newCountyInfo.zips = newCountyZips;
newCountyInfo.branchID = newCountyBranchID;
Run Code Online (Sandbox Code Playgroud)
所以我在数组中有四个元素.然后我将newCountyInfo传递给另一个函数来拉出元素以便在一些HTML元素中显示.
我知道如何获取使用它们的函数中的各个元素的唯一方法是:
JSON.parse(JSON.stringify(newCountyValidation)).name
JSON.parse(JSON.stringify(newCountyValidation)).state
... etc...
Run Code Online (Sandbox Code Playgroud)
必须有更好/更短/更优雅的方式来做到这一点!
它是什么?