简体   繁体   English

jQuery单选按钮如何禁用文本字段而不影响其他

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

I need help. 我需要帮助。 Im having an issue in jquery that when you click the radio button it should not be affected by the others. 我在jquery中遇到问题,当您单击单选按钮时,它不应受其他按钮的影响。

This is my code: 这是我的代码:

<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>

Website: 网站:

http://devsrver.com/ http://devsrver.com/

If you are using selector input[type='radio'] it will affected to all the radio buttons. 如果使用选择器input[type='radio'] ,它将影响所有单选按钮。 You can try like this: 您可以这样尝试:

<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>

You have to add difference attribute on every difference section such as A_Director, B_Director, C_Director. 您必须在每个差异部分(例如A_Director,B_Director,C_Director)上添加差异属性。 But same attribute on same section. 但是同一部分的属性相同。 For example: 例如:

<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'> 

and javascript should be: 和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>

I found 2 problem in your code. 我在您的代码中发现2个问题。

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

  2. jQuery( ".txtfield" ).prop will affect all input field having class txtfield jQuery( ".txtfield" ).prop将影响所有具有类txtfield输入字段


There are some other good ways to do it if you agree to modify your html and css but this is the simple Change in js code. 如果您同意修改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 );
       }
    });

Similarly you have to do it for radio-744 and radio-745 同样,对于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