我有一个文本框和一个按钮,我在按钮上编写了下面的代码。问题是假设我首先在文本框中输入了 10 ,但当我再次输入 10 时,它打印的值不在数组中。所以请帮我解决问题......
jQuery(document).ready(function()
{
jQuery("#mybutton").live('click',function ()
{
var sel_fam_rel=jQuery("#my_textbox").val();
var ids = [];
code =sel_fam_rel;
if($.inArray(code,ids) >= 0)
{
alert("Value is in array");
}
else
{
alert("Value is not in array");
ids.push(code);
}
});
});
最佳答案
这一行:
if($.inArray(code,ids) >= 0)
应更改为:
if($.inArray(code,ids) != -1)
并将您的 ids var 放在 click 之外。
尝试下面的代码片段。
var ids = [];
jQuery("button").on('click', function() {
var sel_fam_rel = jQuery("#my_textbox").val();
code = sel_fam_rel;
if ($.inArray(code, ids) != -1) {
alert("Value is in array");
} else {
alert("Value is not in array");
ids.push(code);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='my_textbox'>
<button>check</button>
关于javascript - 如何使用 Jquery 或 javascript 检查输入的值是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29248812/