繁体   English   中英

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将影响所有具有类txtfield输入字段


如果您同意修改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 );
               }
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM