我需要检查click事件的空输入,如果为空,则停止脚本。发生的事情是脚本仍然发布到php脚本并完全忽略我的代码。我使用了if语句进行检查,但很明显,它并不正确。我可能还需要别的吗?
如果有人能指出我的错误,我将不胜感激。感谢
<script type="text/javascript">
$(function () {
$('#srcRslt').click(function (e) {
e.preventDefault();
if ($('#srcBoxRslt').length == 0) { <--- This the problem area
notif({
type: "error",
msg: "<b>ERROR:<br /><br />You must enter a box to destroy</b>",
height: 99,
multiline: true,
position: "middle,center",
fade: true,
timeout: 3000
});
return false;
}
var value = $('#srcBoxRslt').val();
//alert(value);
var qString = 'sub=' + value;
//alert(qString);
$.post('bdSrchadmin.php', qString, processResponse);
});
function processResponse(data) {
//$('#resultsGoHere').html(data);
//$('#boxdest').empty();
//$("#boxdest").append("<option value='" + data + "'>" + data + "</option>");
// $('#srcBoxRslt').val(data);
$("#submit").prop("disabled", false);
$("#submit2").prop("disabled", false);
$("#submit3").prop("disabled", false);
$('#srcBoxRslt').val('');
$('#srcBox').val('');
notif({
type: "error",
msg: "<b>SUCCESS:<br /><br />You have destroyed boxes.</b>" + data,
height: 99,
multiline: true,
position: "middle,center",
fade: true,
timeout: 3000
});
$("#boxdest").load("bdrefreshBox.php");
};
//$('#boxdest').append('<option value="'.data.'" selected="selected">data</option>');
//$('#boxdest').append(data);
//alert(data);
});
</script>
答案 0 :(得分:3)
$('#srcBoxRslt').length == 0
应该是
$('#srcBoxRslt').val().length == 0
另外,也许修剪空白