jQuery无法读取输入值

input value not getting read by jQuery

本文关键字:输入 读取 jQuery      更新时间:2023-09-26

我花了几天时间寻找PHP中的错误,却发现我的jQuery从来没有通过AJAX传递正确的值。我100%确定这是有效的,并且在过去进行了测试:

JS:

var barcode = null;
if ($("#barcode").length)
 { var $barcode = $("#barcode").val(); console.log("barcode"); }
alert(barcode);
HTML:

<div class="col-lg-10">
   <input type="text" class="form-control" id="barcode" placeholder="Barcode">
</div>

当条形码输入中有值时,为什么条形码仍然等于null ?

因为你要赋值的变量是

$barcode

and you alert

barcode

就像现实生活中的$一样

您正在if块中创建一个名为$barcode的新变量。您的工作人员不知道您指的是上面定义的barcode变量。相反,尝试barcode =而不是var $barcode=

您正在警告为空的条形码。您需要$barcode