如果字段为空,禁用单选按钮



我有一个asp.net网站,在联系我们的表格上,有2个字段(电话号码:,电子邮件地址)和2个单选按钮(首选的联系方式-电话/电子邮件)。我想要的是在这些字段为空时禁用单选按钮,并在它们中有值时启用它们。

我希望单选按钮成为启用当用户开始输入这些字段,如果他们然后删除值,为他们被禁用,所以我知道它需要使用JQuery或JavaScript,但不知道如何做到这一点。

 <div class="form-group">
        <asp:Label ID="TelFieldLabel" class="col-md-3 control-label" runat="server" Text="Contact No." AssociatedControlID="TelField"></asp:Label>
        <div class="col-md-3">
            <asp:TextBox ID="TelField" runat="server" class="form-control" type="Number"></asp:TextBox>
        </div>
    </div>
   <div class="form-group">
        <asp:Label ID="EmailFieldLabel" class="col-md-3 control-label" runat="server" Text="Email address" AssociatedControlID="EmailField"></asp:Label>
        <div class="col-md-3">
            <asp:TextBox ID="EmailField" runat="server" class="form-control"></asp:TextBox>
        </div>
        <div class="col-md-offset-3 col-md-9">
            <asp:RequiredFieldValidator Display="Dynamic" runat="server" ID="RequiredFieldValidator2" SetFocusOnError="true" ForeColor="Red" ControlToValidate="EmailField" ErrorMessage="Please enter your email address." />
            <asp:RegularExpressionValidator Display="Dynamic" runat="server" ID="RegularExpressionValidator1" SetFocusOnError="True" ForeColor="Red" ControlToValidate="EmailField" ErrorMessage="RegularExpressionValidator" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*">Email address is not a valid format.</asp:RegularExpressionValidator>
        </div>
    </div>    
<asp:Label runat="server" class="radio-inline">
         <asp:RadioButton runat="server" id="phone" value="Phone" GroupName="prefcontact"/> Phone
    </asp:Label>
    <asp:Label runat="server" class="radio-inline">
         <asp:RadioButton runat="server" id="email" value="Email" GroupName="prefcontact"/> Email
    </asp:Label>

使用下面的代码片段

$('input[type="text"]').bind('blur', function(e) {
  if($('#telNo').val().length > 0 || $('#email').val().length > 0){
  
    $('input[type="radio"]').prop('disabled', false);  
    
  }else {
    $('input[type="radio"]').prop('disabled', true);  
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>
  Tel. No : <input type="text" id="telNo" /><br >
  Email : <input type="text" id="email" /><br>
  
  Preferred contact method : 
  <input type="radio" name="preferred" disabled />Tel
  &nbsp;&nbsp;&nbsp;
  <input type="radio" name="preferred" disabled />Email
</div>

参考此工作小提琴:http://jsfiddle.net/xqfr4na6/1/

你可以使用input propertychange paste来改变单选按钮的状态,而在文本框中输入。有了这个解决方案,你不需要等待控件离开文本框,blur函数做。

    $('input[type="text"]').on('input propertychange paste', function () {
    if ($('#telNo').val().length > 0 || $('#email').val().length > 0) {
        $('input[type="radio"]').prop('disabled', false);
    } else {
        $('input[type="radio"]').prop('disabled', true);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>Tel No.:
    <input type="text" id="telNo" />&nbsp;Email Address:
    <input type="text" id="email" />
    <br/>
    <br/>
    <input type="radio" name="preferred" disabled />Phone
    <input type="radio" name="preferred" disabled />Email</div>

您可以使用Jquery这样做。

$('#txtPhone').on('input',function(e){
 if($('#txtPhone').val().length == 0){
 $("#phone input:radio").attr('disabled',true);
 $("#email input:radio").attr('disabled',true);
 }
else{
$( '#phone input[type="radio"]' ).removeAttr( "disabled" );
$( '#email input[type="radio"]' ).removeAttr( "disabled" );
} 
});

请查看这篇文章的修复我发现(JQuery不禁用我的单选按钮)

最新更新