javascript jquery 选择单选按钮

标签 javascript php jquery html css

我有一个输出许多 div 的 php 脚本,在每个 div 中都有一个具有特定值的单选按钮。我想通过在单击 div 时选择单选按钮来创建更好的用户体验,这在我使用 jquery 时不是什么大问题。问题是,当单击单选按钮本身而不是 div 时,以下单击不再具有选择单选按钮的效果。我还尝试在检查选定的单选按钮之前删除每个单选按钮上的选中属性,但这不起作用。这是我的做法:

$(".fontCellDiv").click(function(){ 
     $(this).find("input").attr("checked", true);
 });

有人有想法吗?

最佳答案

使用.prop()而不是 .attr()

来自文档:

Attributes vs. Properties

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

关于javascript jquery 选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053679/

相关文章:

javascript - 在 CSS 中使用 Javascript/jQuery 使 border-right 镜像 border-bottom?

javascript - 并非所有坐标都使用平行坐标 (d3) 绘制

javascript - React - 获取文本区域的值?

php - 如何从一个输入 Html 表单插入数组字符串并从中获取另一数据

php - 扩展 mysqli_result - 它使用 store_result() 还是 use_result()

javascript - iframe点击使div显示隐藏

javascript - 如何重复通过ajax检索数据的文本字段

javascript - 函数只能作用于一个div?

javascript - 添加或更新事件后,Angular 引导日历不会更新 View

php - TCPDF创建PDF花费太多时间,如何在这段时间显示加载效果