【发布时间】:2013-07-24 21:34:02
【问题描述】:
我正在使用ScrewDefaultButtons jQuery 插件来替换带有图像的复选框。该插件使用以下代码成功隐藏复选框并将其替换为选中/未选中的图像:
$('input:checkbox').screwDefaultButtons({
image: "url(../../images/radio_check.png)",
width: 14,
height: 14
});
我遇到的问题是在尝试将这些自定义复选框与使用复选框将项目标记为已完成的待办事项列表插件结合使用时出现的。当一个项目完成时,复选框被勾选,该项目通过 PHP 和 AJAX 移动到“已完成项目”列表中。
上面的代码成功地替换了默认复选框W /自定义图像,但勾选新复选框时,除了“未选中的”图像更改为“已选中”图像之外不会发生任何内容。使用默认复选框,会弹出一个警报,确认您是否要移动项目,如果确认,则移动项目。
最大的问题是:如何使用我的待办事项列表的复选框功能让螺丝默认按钮复选框工作?
这是复选框的代码:
默认复选框的输出:
<td id="checkbox_parent">
<input id="ctdl-1852" class="todo-checkbox uncompleted" type="checkbox"></input>
<input type="hidden" value="a193d8dc3a" name="cleverness_todo_complete_nonce"></input>
</td>
ScrewDefaultButton 复选框的输出:
<td id="checkbox_parent">
<div class="todo-checkbox uncompleted styledCheckbox" style="background-image: url("../../images/radio_check.png"); width: 14px; height: 14px"> … </div>
<input type="hidden" value="a193d8dc3a" name="cleverness_todo_complete_nonce"></input>
</td>
PHP:
protected function show_checkbox( $id, $completed = NULL, $layout = 'table', $single = '' ) {
$permission = CTDL_Lib::check_permission( 'todo', 'complete' );
if ( $permission === true ) {
if ( is_admin() || $layout == 'table' ) $this->list .= '<td id="checkbox_parent">';
if ( $completed == 1 ) {
$this->list .= sprintf( '<input type="checkbox" id="ctdl-%d" class="todo-checkbox completed'.$single.'" checked="checked" />', esc_attr( $id ) );
} else {
$this->list .= sprintf( '<input type="checkbox" id="ctdl-%d" class="todo-checkbox uncompleted'.$single.'" />', esc_attr( $id ) );
}
$cleverness_todo_complete_nonce = wp_create_nonce( 'todocomplete' );
$this->list .= '<input type="hidden" name="cleverness_todo_complete_nonce" value="'.esc_attr( $cleverness_todo_complete_nonce ).'" />';
if ( is_admin() || $layout == 'table' ) $this->list .= '</td>';
}
}
jQuery:(注意:添加.bind("click", function () { 不能解决问题)
$( '.todo-checkbox' ).click( function () {
var confirmed = confirm( ctdl.CONFIRMATION_CHECK );
if ( confirmed == false ) return false;
var status = 1;
var id = $( this ).attr( 'id' ).substr( 5 );
var todoid = '#todo-' + id;
var single = $ ( this ).hasClass( 'single' );
if ($( this ).prop( 'checked' ) == false ) status = 0;
var data = {
action: 'cleverness_todo_complete',
cleverness_id: id,
cleverness_status: status,
_ajax_nonce: ctdl.NONCE
};
jQuery.post( ctdl.AJAX_URL, data, function( response ) {
if ( single != true ) {
$( todoid ).fadeOut( function () {
$( this ).remove();
});
$( '.todo-checkbox' ).prop( "checked", false );
}
} );
} );
编辑(2013 年 7 月 28 日):输出 Hauke 的第一个代码:
<td id="checkbox_parent">
<div class="todo-checkbox uncompleted styledCheckbox" style="background-image: url("../../images/radio_check.png"); width: 14px; height: 14px; cursor: pointer; background-position: 0px 0px;"> … </div>
<input type="hidden" value="a81d045af8" name="cleverness_todo_complete_nonce"></input>
</td>
当复选框被点击时,输出的唯一区别是背景位置的高度变为-14px。
【问题讨论】:
-
是您的代码或脚本,在 $( '.todo-checkbox' ).click() 之前或之后调用“screwDefaultButtons”的代码在哪里?如果是之后,很可能您没有绑定点击事件,因为您附加了新的 DOM 元素,是的,您确实有此事件所需的 css 类,但您将需要
.bind('click',function(){}); or on.('click',function(){});以防万一, 在点击事件绑定之前放脚本 -
它是在之后,但是我只是将它移到
$( '.todo-checkbox' ).click()之前,并且出现了警报提示(以前没有发生过),但是如果我单击确定移动该项目,则没有任何反应。有了.bind('click',function(){});,我应该把它放在哪里? -
尝试代替
.click(function()放置.bind('click',function() -
刚刚试了,和以前一样没动作。
标签: php jquery ajax input checkbox