【问题标题】:What is the correct way to check for empty value in input field [duplicate]检查输入字段中的空值的正确方法是什么[重复]
【发布时间】:2017-09-13 04:55:23
【问题描述】:

我需要检查点击事件中的空输入,如果为空则停止脚本。发生的事情是脚本仍在发布到 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>

【问题讨论】:

  • $('#srcBoxRslt').length 返回#srcBoxRslt 选择的元素数量(可能是1)。另外,请使用搜索。

标签: javascript jquery


【解决方案1】:
$('#srcBoxRslt').length == 0

应该是

$('#srcBoxRslt').val().length == 0

另外,也许修剪空白

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多