作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这个 jQuery,它允许在单击按钮事件时向表中添加新行,但前提是尚未添加 id
值。
var factAgregada = [];
$('#btnAgregarFactura').on("click", function (e) {
e.preventDefault();
// no_factura and fecha_factura comes from a modal window
// it's not relevant to show that part here
var no_factura = $("#no_factura").val(),
fecha_factura = $("#fecha_factura").val(),
html;
console.log(no_factura);
console.log(factAgregada.indexOf(no_factura));
if (factAgregada.indexOf(no_factura) >= -1) {
html = '<tr>';
html += '<td><input type="checkbox" id="' + no_factura + '"></td>';
html += '<td>' + no_factura + '</td>';
html += '<td>' + fecha_factura + '</td>';
html += '</tr>';
$(html).appendTo("#facturaBody");
}
factAgregada.push(no_factura);
});
在第一次执行时,代码运行良好,console.log()
输出如下:
12
-1
所以添加行是正确的,但是在第二次执行并保持相同的值 console.log()
从以前的值更改为这个值:
12
0
因此,不应该添加行但仍然添加到表中并继续执行 appendTo()
,那里有什么问题?我没看到什么?
最佳答案
var factAgregada = [];
$('#btnAgregarFactura').on("click", function (e) {
e.preventDefault();
var no_factura = 10,
fecha_factura = 2,
html;
console.log(no_factura);
console.log(factAgregada.indexOf(no_factura));
if (factAgregada.indexOf(no_factura) == -1) {
html = '<tr>';
html += '<td><input type="checkbox" id="' + no_factura + '"></td>';
html += '<td>' + no_factura + '</td>';
html += '<td>' + fecha_factura + '</td>';
html += '</tr>';
console.log(html);
}
factAgregada.push(no_factura);
});
改变条件检查
关于javascript - 如果 indexOf 返回 0,为什么这段代码仍然向表中添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923268/
我的 WordPress 数据库中有一个名为“post_votes”的自定义表,用于投票帖子。 每当我发布新帖子时,我是否可以同时向该表添加一些数据? 我试图在不编辑任何 wp 核心文件的情况下实现这
我对 Rails 非常陌生,完全被难住了。我真的也需要这个工作,因为它是另一个更大的主要是 Javascript 项目的一部分,而 Rails 部分刚刚被甩给了我。我必须向 page_events 表
我是一名优秀的程序员,十分优秀!