尝试更改input元素的属性并将其替换为hidden

时间:2017-04-27 18:36:25

标签: javascript jquery

我正在尝试做一个小修改,在下拉列表中选择YES / NO时会运行以下代码

$(document).on('#is_taxable','change',function() {
    var data = $(this).val();
    alert(data);
    if(data == 'true') {
        $("#tax_authority_id").attr('type','hidden');
        $("#tax_exemption_id").attr('type','hidden');
        $("Tax Authority is hidden because it is not required when taxable is 'YES'").insertAfter("label['for=inputtax_authority_id']");
        $("Tax Exemption is hidden because it is not required when taxable is 'YES'").insertAfter("label['for=tax_exemption_id']");
    }else {
        $("#tax_authority_id").attr('type','text');
        $("#tax_exemption_id").attr('type','text');
        $("#tax_id_value").attr('type','hidden');
        $("Tax Value is hidden because it is not required when taxable is 'NO'").insertAfter("label['for=inputtaxitem']");
    }
});

我正在尝试将输入字段的attr更改为隐藏,因此在东方时它们仍然存在,而表单在提交到服务器端时需要它,我无法禁用它们

我如何解决,它只是没有改变它,我在这里错过了什么

1 个答案:

答案 0 :(得分:0)

改为使用.hide().show()。像

$("#tax_authority_id").hide();
$("#tax_authority_id").show();

您的代码

$(document).on('#is_taxable','change',function() {
    var data = $(this).val();
    alert(data);
    if(data == 'true') {
        $("#tax_authority_id").hide();
        $("#tax_exemption_id").hide();
        $("Tax Authority is hidden because it is not required when taxable is 'YES'").insertAfter("label['for=inputtax_authority_id']");
        $("Tax Exemption is hidden because it is not required when taxable is 'YES'").insertAfter("label['for=tax_exemption_id']");
    }else {
        $("#tax_authority_id").show();
        $("#tax_exemption_id").show();
        $("#tax_id_value").hide();
        $("Tax Value is hidden because it is not required when taxable is 'NO'").insertAfter("label['for=inputtaxitem']");
    }
});