Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript阻止单选按钮工作_Javascript_Jquery_Html - Fatal编程技术网

Javascript阻止单选按钮工作

Javascript阻止单选按钮工作,javascript,jquery,html,Javascript,Jquery,Html,我认为这很简单,但我不明白为什么不能单击#type2 <input type="radio" id="type1" name="group1" checked> <input type="radio" id="type2" name="group1"> <input type="text" class="type1"> <input type="text" class="type2" disabled> <script> fu

我认为这很简单,但我不明白为什么不能单击
#type2

<input type="radio" id="type1" name="group1" checked>

<input type="radio" id="type2" name="group1">

<input type="text" class="type1">

<input type="text" class="type2" disabled>

<script>

function toggle() {
    if ($('#type1').attr('checked', true)) {
        $('.type1').attr('disabled', false);
        $('.type2').attr('disabled', true);
    }
    else if ($('#type2').attr('checked', true)) {
        $('.type1').attr('disabled', true);
        $('.type2').attr('disabled', false);
    }
}

$(document).ready(function() {
    toggle();
});

</script>

函数切换(){
if($('#type1').attr('checked',true)){
$('.type1').attr('disabled',false);
$('.type2').attr('disabled',true);
}
else if($('#type2').attr('checked',true)){
$('.type1').attr('disabled',true);
$('.type2').attr('disabled',false);
}
}
$(文档).ready(函数(){
切换();
});
我看不出有什么问题。任何帮助都会很棒,谢谢。

试试:

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').attr('disabled', false);
        $('.type2').attr('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').attr('disabled', true);
        $('.type2').attr('disabled', false);
    }
}
示例:

您使用的版本正在将
#type1
已选中的属性设置为true。您可以使用、和来确定是否选中了复选框/单选按钮。

尝试:

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').attr('disabled', false);
        $('.type2').attr('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').attr('disabled', true);
        $('.type2').attr('disabled', false);
    }
}
示例:


您使用的版本正在将
#type1
已选中的属性设置为true。您可以使用、和来确定是否选中了复选框/单选按钮。

您使用的是.attr而不是.prop

.attr代表属性,.prop代表属性。属性=选中、选中、禁用

文件:

要确定是否选中复选框

if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )

您使用的是.attr而不是.prop

.attr代表属性,.prop代表属性。属性=选中、选中、禁用

文件:

要确定是否选中复选框

if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )
工作演示**

需要纠正的事情很少
:)

  • 使用
    .is(“:checked”)
  • 将其附加到单击事件
  • 相应地附加和分离禁用的
    属性
希望它符合原因
:)

代码

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').removeAttr('disabled');
        $('.type2').attr('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').attr('disabled', true);
        $('.type2').removeAttr('disabled');
    }
}

$(document).ready(function() {
    $("input[type=radio]").click(function() {
        toggle();
    });
});​
工作演示**

需要纠正的事情很少
:)

  • 使用
    .is(“:checked”)
  • 将其附加到单击事件
  • 相应地附加和分离禁用的
    属性
希望它符合原因
:)

代码

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').removeAttr('disabled');
        $('.type2').attr('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').attr('disabled', true);
        $('.type2').removeAttr('disabled');
    }
}

$(document).ready(function() {
    $("input[type=radio]").click(function() {
        toggle();
    });
});​

您未检查此处的条件

您正在设置它,这总是正确的。。 试试这个

if ($('#type1').is(':checked')){
也可以使用
.prop()
而不是
.attr()
,因为这实际上不是一个属性,而是一个单选按钮的属性。 您的代码应该如下所示

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').prop('disabled', false);
        $('.type2').prop('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').prop('disabled', true);
        $('.type2').prop('disabled', false);
    }
}

您未检查此处的条件

您正在设置它,这总是正确的。。 试试这个

if ($('#type1').is(':checked')){
也可以使用
.prop()
而不是
.attr()
,因为这实际上不是一个属性,而是一个单选按钮的属性。 您的代码应该如下所示

function toggle() {
    if ($('#type1').is(':checked')) {
        $('.type1').prop('disabled', false);
        $('.type2').prop('disabled', true);
    }
    else if ($('#type2').is(':checked')) {
        $('.type1').prop('disabled', true);
        $('.type2').prop('disabled', false);
    }
}