Jquery 关于如何在不影响其他人的情况下禁用文本字段的单选按钮

Jquery radio button on how to disabled the text field without affecting others

我需要帮助。我在 jquery 中遇到一个问题,当您单击单选按钮时,它不应受到其他单选按钮的影响。

这是我的代码:

<script>
    jQuery("input[type='radio']").change(function(){
        if(jQuery(this).val() == "Not Vote Anyone")
        {
            jQuery( ".txtfield" ).prop( "disabled", false );
        } else {
            jQuery( ".txtfield" ).prop( "disabled", true );
        }
    });
</script>

网站:

http://devsrver.com/

如果您使用选择器input[type='radio'],它将影响所有单选按钮。你可以这样尝试:

<script>
    jQuery("input[name='radio-743']").change(function(){
       if(jQuery(this).val() == "Not Vote Anyone"){
          jQuery( ".txtfield" ).prop( "disabled", false );
       } else {
          jQuery( ".txtfield" ).prop( "disabled", true );
       }
    });
</script>

您必须在每个差异部分添加差异属性,例如 A_Director、B_Director、C_Director。但同一部分的相同属性。例如:

<input type='radio' groupsection='A_Direction'>Note Vote Anymore
<input type='radio' groupsection='A_Direction'>John Doe
<input type='radio' groupsection='A_Direction'>Jane Doe
<input type='text' groupsection='A_Direction'> 

<input type='radio' groupsection='B_Direction'>Note Vote Anymore
<input type='radio' groupsection='B_Direction'>John Doe
<input type='radio' groupsection='B_Direction'>Jane Doe
<input type='text' groupsection='B_Direction'> 

和javascript应该是:

<script>
    jQuery("input[type='radio']").change(function(){
        var groupsection = $(this).attr('groupsection');
        if(jQuery(this).val() == "Not Vote Anyone")
        {
            jQuery( ".txtfield[groupsection='"+groupsection+"']" ).prop( "disabled", false );
        } else {
            jQuery( ".txtfield[groupsection='"+groupsection+"']" ).prop( "disabled", true );
        }
    });
</script>

我在你的代码中发现了 2 个问题。

  1. input[type='radio'] 将影响所有 radio.

  2. jQuery( ".txtfield" ).prop 将影响具有 class txtfield

  3. 的所有输入字段

如果您同意修改 html 和 css,还有一些其他好的方法可以做到这一点,但这是 js 代码中的简单更改。


    jQuery("input[name='radio-743']").change(function(){
       if(jQuery(this).val() == "Not Vote Anyone"){
          jQuery("input[name='RifleDirectorName']").prop( "disabled", false );
       } else {
          jQuery("input[name='RifleDirectorName']").prop( "disabled", true );
       }
    });

同样,您必须为 radio-744radio-745

执行此操作
jQuery("input[name='radio-744']").change(function(){
           if(jQuery(this).val() == "Not Vote Anyone"){
              jQuery("input[name='PistolDirectorName']").prop( "disabled", false );
           } else {
              jQuery("input[name='PistolDirectorName']").prop( "disabled", true );
           }
        });




 jQuery("input[name='radio-745']").change(function(){
               if(jQuery(this).val() == "Not Vote Anyone"){
                  jQuery("input[name='ShotgunDirectorName']").prop( "disabled", false );
               } else {
                  jQuery("input[name='ShotgunDirectorName']").prop( "disabled", true );
               }
            });