javascript - jquery 单选按钮单击和更改功能不起作用

标签 javascript jquery html

我想根据单选按钮值隐藏和显示 div。 HTML 代码是,

<div id="share_to_others">
  <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type">
</div>

我尝试过的 jquery 代码是,

$("#share_to_others input[name='fx_sharepl_type']").click(function () {
    alert("test");
});

$("#share_to_others input[name='fx_sharepl_type']").change(function () {
    alert("test");
});

$("#fx_sharepl_type").change(function () {
    alert("asdas");
});

$("input[name=fx_sharepl_type]:radio").change(function () {
    alert("click fired");   
});

$(document).on('change', 'input:radio[name=fx_sharepl_type"]', function (event) {
    alert("click fired");
});

其中许多来自 jsfiddle 工作演示,但不适合我,我不知道为什么。 我做错了什么吗?

最佳答案

您必须为每个单选按钮提供唯一的 id,然后像这样进行操作。

$("#r1, #r2, #r3").change(function () {

关于javascript - jquery 单选按钮单击和更改功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206463/

相关文章:

javascript - 如何让一个 div 展开全宽同时隐藏另一个 div

javascript - 单击复选框时禁用/启用表单文本框,并将文本框的名称作为参数传递给 JavaScript 函数

css - div in li 是错误的用法吗?

javascript - 如何显示动态添加内容的提示框?

javascript - 如何展平已发布的 NPM 模块的文件结构

javascript - 如何在使用 JavaScript/JQuery 单击单选按钮时突出显示表格单元格 (TD)?

javascript - Alertify.log 中的 "type"是什么意思(通知,类型,等待);在alertify.js中

javascript - 如何用英镑符号(货币)替换问号

javascript - 特定对象实例的自定义函数 - 我有什么可能性?

php - 使用Ajax将order_id传递到php并插入到数据库