gpt4 book ai didi

javascript - 克隆时 jQuery UI 复选框行为异常

转载 作者:行者123 更新时间:2023-11-29 20:00:21 24 4
gpt4 key购买 nike

我正在尝试创建一个输入表,当您在底行的其中一个输入中输入文本时,该表会自动添加一个新行。在大多数情况下,它工作正常。但是,我在使用 jQuery UI 复选框按钮时遇到了一些问题。

复选框按钮应该在被点击时改变它们的图标。这适用于原始按钮,但添加新行时出现的克隆按钮无法正常工作。

可以看到in jsfiddle here .要重现该问题,请在第三个输入中放置一些文本。您会看到出现第四行。如果您按下第四个复选框,您会看到第三个复选框的图标发生变化。错误的按钮也获得了 ui-state-focus 但实际上并没有获得焦点,这让我很困惑,尽管正确的按钮确实获得了 ui-state-active 并且据我所知似乎评估为已检查

需要明确的是,这两个复选框具有相同的 ID,并且它们的标签用于正确的复选框 - createNewRow() 函数会处理这一点。如果您注释掉将复选框变成 jQuery UI 复选框的行,您将看到一切正常。如果你在 buttonSwitchCheck 函数中 console.log $(this).attr('id') 的值,你会看到它也有正确的 ID - 如果你点击第四个按钮,它会告诉你$(this) 的 id 是“test4”,但它是“test3”(第三个按钮)获取图标更改。

我盯着这个看得快要发疯了,我很感激人们能提供的任何帮助。这是代码:

// Turns on and off an icon as the checkbox changes from checked to unchecked.
function buttonSwitchCheck() {
if ($(this).prop('checked') === true) {
$(this).button("option", "icons", {
primary: "ui-icon-circle-check"
});
} else {
$(this).button("option", "icons", {
primary: "ui-icon-circle-close"
});
}
}

// Add a new row at the bottom once the user starts filling out the bottom blank row.
function createNewRow() {
// Identify the row and clone it, including the bound events.
var row = $(this).closest("tr");
var table = row.closest("table");
var newRow = row.clone(true);
// Set all values (except for buttons) to blank for the new row.
newRow.find('.ssheet').not('.button').val('');
// Find elements that require an ID (mostly elements with labels like checkboxes) and increment the ID.
newRow.find('.ssheetRowId').each(function () {
var idArr = $(this).attr('id').match(/^(.*?)([0-9]*)$/);
var idNum = idArr[2] - 0 + 1;
var newId = idArr[1] + idNum;
$(this).attr('id', newId);
$(this).siblings('label.ssheetGetRowId').attr('for', newId);
});
// Add the row to the table.
newRow.appendTo(table);
// Remove the old row's ability to create a new row.
row.removeClass('ssheetNewRow');
row.find(".ssheet").unbind('change', createNewRow);
}

$(document).ready(function () {
// Activate jQuery UI checkboxes.
$(".checkButton").button().bind('change', buttonSwitchCheck).each(buttonSwitchCheck);

// When text is entered on the bottom row, add a new row.
$(".ssheetNewRow").find(".ssheet").not('.checkButton').bind('change', createNewRow);
});

编辑: 我找到了一个解决方案,我将与大家分享。感谢下面的“Funky Dude”,他启发了我开始沿着正确的轨道思考。

诀窍是在克隆之前销毁原始行中的 jQuery UI 按钮,然后立即为原始行和副本重新初始化它。您不需要取消绑定(bind)和重新绑定(bind)更改事件 - 它只是有问题的 jQuery UI 按钮。在 createNewRow 函数中:

row.find('.checkButton').button('destroy');
var newRow = row.clone(true);
row.find('.checkButton').add(newRow.find('.checkButton')).button().each(buttonSwitchCheck);

最佳答案

尝试使用较新的方法 .on,它允许委托(delegate),这应该有助于动态更改您的 DOM:

$(".checkButton").button().each(buttonSwitchCheck);
$("table").on("change", ".checkButton", buttonSwitchCheck);

我不确定,但它可能有助于不必担心将事件绑定(bind)到特定元素。

此外,您可以将它用于文本框 change 事件:

$("table").on("change", ".ssheetNewRow .ssheet:not(.checkButton)", createNewRow);

这是你对我所做的更改的摆弄:http://jsfiddle.net/Cugb6/3/

它的功能没有任何不同,但对我来说,它更简洁一些。我以为它会解决你的问题,但显然没有,因为 button 小部件有问题。

有趣的是,他们似乎并不“支持”克隆:http://bugs.jqueryui.com/ticket/7959

关于javascript - 克隆时 jQuery UI 复选框行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com