【问题标题】:jquery check for checked inputjquery 检查已检查的输入
【发布时间】:2014-11-26 10:54:44
【问题描述】:

我很少有具有唯一 ID 的无线电输入。现在我有一个脚本,如果其中一个被选中,它会显示一些额外的内容。有用。但是当我检查另一个无线电输入时,我想清除那些额外的内容。 我认为这段代码不起作用。没有清除多余的内容。

 $(document).ready(function () {
    $("#delivery_option_5_1").change(function () {
        if ($(this).attr("checked")) {
            $(".delivery_options").after("<p id='extracontent'>Hello!</p>");
        }
        else {
            $("#extracontent").remove();
        }
    });
});

【问题讨论】:

  • 您能否编辑您的问题以包含您的 HTML。
  • 请创建一个 JSFiddle。
  • 你应该使用if ( this.checked ) { ...,当你选中该框时,属性永远不会改变。
  • 你可以使用if ($(this).is(":checked")){
  • 我的 html 无关紧要,因为我需要在不编辑 html 中的任何内容的情况下执行此操作,html 是由 prestashop 生成的。

标签: jquery if-statement insert attr remove-if


【解决方案1】:

总是在添加新之前删除它。也使用prop 而不是attr

$(document).ready(function () {
    $("#delivery_option_5_1").change(function () {

        $("#extracontent").remove();

        if ($(this).prop("checked")) {
            $(".delivery_options").after("<p id='extracontent'>Hello!</p>");
        }
    });
});

【讨论】:

  • 抱歉,还是什么都没有。唯一的改进是当我检查另一个收音机并再次检查 #delivery_option_5_1 时,taht 脚本没有添加另一个 #extracontent 元素,但是当我检查其他收音机输入时它没有删除那个 #extracontent。
  • 您是否只想在#delivery_option_5_1 上添加额外的内容 div? #delivery_option_5_1 复选框之后的 div delivery_options 也是?您可以将 html 添加到您的问题中吗?
【解决方案2】:

我就是这样做的。

$(document).ready(function () {
    $("#wpunkcie").change(function () {

        if ($(this).prop("checked")) {
            $("#wysylka").after("<p id='info'>Mapa i informacja o wysyłce</p>");
        }
    });
    $("#inne").change(function () {



        if ($(this).prop("checked")) {
            $("#info").remove();
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="wysylka"><input type="radio" name="halo" id="wpunkcie"> Odbiór w punkcie pocztowym</input><br>
    <input type="radio" name="halo" id="inne">Inna forma wysyłki</input></form>

谢谢帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多