单选按钮未选择

时间:2018-09-10 16:58:48

标签: javascript jquery html razor

我使用的是剃须刀单选按钮,但未选择一个单选按钮 未禁用不知道为什么不选择Single radio button

enter image description here

       <div class="col-md-4" style="margin-right:-22%;">
             <div class="form-group">
                 <span> Single </span> @Html.RadioButton("Type", "1", new { onchange = "GetType(this.value)", @required = "required" })
              </div>
          </div>
       <div class="col-md-3">
           <div class="form-group">
               <span> Multiple </span> @Html.RadioButton("Type", "2", new { onchange = "GetType(this.value)", @required = "required" })
                </div>
        </div>

jQuery代码

  function GetType(Type)
            {
                if (Type == 1) {
                    $("#AllDepartments").hide();
                    $("#AllEmployees").show();
                }
                else if(Type == 2)
                {
                    $("#AllEmployees").hide();
                    $("#AllDepartments").show();
                }
            }

1 个答案:

答案 0 :(得分:0)

您的描述太低了,但是我想这个例子会让您理解,因为单选按钮必须具有相同的名称,例如:

<input type="radio" name="asdf">
<input type="radio" name="asdf">

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>radio demo</title>
  <style>
  textarea {
    height: 25px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<form>
  <input type="button" value="Input Button">
  <input type="checkbox">
  <input type="file">
  <input type="hidden">
  <input type="image">
  <input type="password">
  <input type="radio" name="asdf">
  <input type="radio" name="asdf">
  <input type="reset">
  <input type="submit">
  <input type="text">
  <select>
    <option>Option</option>
  </select>
  <textarea></textarea>
  <button>Button</button>
</form>
<div></div>
 
<script>
var input = $( "form input:radio" )
  .wrap( "<span></span>" )
  .parent()
    .css({
      background: "yellow",
      border: "3px red solid"
    });
 
$( "div" )
  .text( "For this type jQuery found " + input.length + "." )
  .css( "color", "red" );
 
// Prevent form submission
$( "form" ).submit(function( event ) {
  event.preventDefault();
});
</script>
 
</body>
</html>