繁体   English   中英

如何切换内容?

[英]How to toggle content of <td>?

我非常希望能够在有 x 和没有 x 之间切换

<td class="checkbox"> <input name="signed" type="checkbox" checked /> x </td>

例如,参见 JSFiddle

http://jsfiddle.net/littlesandra88/GMSCW/

按下提交时触发的代码是

$('form').live('submit', function(){
   $.post($(this).attr('action'), $(this).serialize(), function(response){
         // do something here on success
   },'json');
   return false;
});

我想我必须做一些事情

$(this).getElementByClass("checkbox").td.toggle('x');

但是checked还是应该在<input name="signed" type="checkbox" checked />中更新

有谁知道如何做到这一点?

将 X 包裹在一个跨度中,然后隐藏它

<td class="checkbox"> 
      <input name="signed" type="checkbox" checked />
      <span id='toggleme'>x</span>
 </td>

 //change the visibility of the x
 $('#toggleme').toggle();

// check the checkbox
$('input[name=signed]').attr('checked', true);

要检查 jQuery 中的复选框,请使用ELEMENT.attr("checked","checked")或 jQuery 1.6 方式和ELEMENT.prop("checked", true)

最好的主意,IMO,是在复选框旁边放一个<label> ,然后用$("input[type=checkbox] + label", this).toggle() toggle它。

要更改复选框,其他用户提供的示例可以正常工作。

http://jsfiddle.net/w5nAr/

这假设您不想更改复选框,只更改它旁边的文本。 如果你想要两者,这是另一个例子:

http://jsfiddle.net/w5nAr/4/

尝试查看这里的代码,它基于 Bryan 的工作。

$({some identifier for your checkbox}).attr("checked",true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM