【问题标题】:RadiobuttonFor always selecting default value in JavascriptRadiobuttonFor 总是在 Javascript 中选择默认值
【发布时间】:2021-02-04 07:35:46
【问题描述】:

列表中有两个 RadiobuttonFor 项目。一个值为“是”,另一个值为“否”。选择“是”时,页面的一部分将可见,而选择“否”时,该部分不应显示。

我面临的问题是,即使选择“否”,通过 Javascript 的值也是“是”。无论选择何种选项,该值始终为“是”。

我尝试了多种以不同方式获取值的方法,但总是得到相同的结果,并且值被拉为“是”。

是否有一些不同的方式来提取正确的值?

MVC HTML:

     <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <div data-toggle="buttons">
                                    <label class="btn btn-radio">
                                        @Html.RadioButtonFor(m => m.Claims, "Yes", new { name = "radClaim" }) Yes
                                    </label>

                                    <label class="btn btn-radio">
                                        @Html.RadioButtonFor(m => m.Claims, "No", new { name = "radClaim" }) No
                                    </label>
                                </div>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                @Html.ValidationMessageFor(m => m.Claims, null, new { id = "clmError" })
                            </div>

Javascript: 即使选择了 No,this.value 和 var selected 也始终为“Yes”。

     $("input[name='Claims']").on("change", function () {
    var selected = $("input[name='Claims']:checked").val()
 
    if (this.value === "Yes") {
        $("#showClaimsHistory").show();
        if (claimCount > 0) {
            $("#showClaimList").show();
        }

    } else {
        $("#showClaimsHistory").hide();
        $("#showClaimList").hide();
    }
});

CSS:

[data-toggle="buttons"] > .btn input[type="radio"],
[data-toggle="buttons"] > .btn-group > .btn input[type="radio"],
[data-toggle="buttons"] > .btn input[type="checkbox"],
[data-toggle="buttons"] > .btn-group > .btn input[type="checkbox"] {
  position: absolute;
  clip: rect(0, 0, 0, 0);
  pointer-events: none;
}


    .btn {
     display: inline-block;
     margin-bottom: 0;
     font-weight: normal;
     text-align: center;
     vertical-align: middle;
     -ms-touch-action: manipulation;
      touch-action: manipulation;
      cursor: pointer;
      background-image: none;
      border: 1px solid transparent;


white-space: nowrap;
  padding: 7px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.btn:focus,
.btn:active:focus,
.btn.active:focus,
.btn.focus,
.btn:active.focus,
.btn.active.focus {
  outline: 5px auto -webkit-focus-ring-color;
  outline-offset: -2px;
}
.btn:hover,
.btn:focus,
.btn.focus {
  color: #555555;
  text-decoration: none;
}
.btn:active,
.btn.active {
  outline: 0;
  background-image: none;
  -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
.btn.disabled,
.btn[disabled],
fieldset[disabled] .btn {
  cursor: not-allowed;
  opacity: 0.65;
  filter: alpha(opacity=65);
  -webkit-box-shadow: none;
  box-shadow: none;
}
a.btn.disabled,
fieldset[disabled] a.btn {
  pointer-events: none;
     }

.btn-radio {
/*border-radius: 30px;*/
border: 1px solid #dcdcdc;
cursor: pointer;
display: inline-block;
font-weight: 600;
/* padding: .75em 1em; */
text-align: center;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
min-width: 110px;
font-size: 16px;
letter-spacing: 2px;
line-height: 40px;
height: 60px;
text-align: center;
text-transform: none;
padding-left: 4.2em !important;
padding-right: 4.2em !important;
margin-right: 1.2em !important;
 }

.btn-radio:hover {
    background: #1e73e9 !important;
    border: 1px solid #1e73e9 !important;
    color: #fff;
}

------------ 编辑 -------------

从下面添加解决方案后的结果:

javascript运行时传入的值:

【问题讨论】:

  • 从两个单选按钮中删除此代码 new { name = "radClaim" }
  • @meysam asadi,我已删除该代码,但仍未解决

标签: javascript jquery asp.net-mvc


【解决方案1】:

试试这个解决方案

 $("input[name='Claims']").on("change", function () {
    var selected = $("input[name='Claims']:checked").val()
 
    if (selected === 'Yes') {
        $("#showClaimsHistory").show();
        if (claimCount > 0) {
            $("#showClaimList").show();
        }

    } else {
        $("#showClaimsHistory").hide();
        $("#showClaimList").hide();
    }
});

【讨论】:

  • 我已经编辑了添加这个作为解决方案后的结果。它的值仍然为“是”
  • 嗯,您的下拉选择语法有问题,这就是为什么它总是选择 YES
  • 下拉列表是从数据库中填充的。但是,是/否单选按钮在此之前起作用。如果选择否,它应该隐藏历史记录和列表(不需要对它们执行任何操作)...所有其他操作都工作正常(从下拉/保存/删除中选择),它只有带有是/否的单选按钮问题来自
【解决方案2】:

代码没有问题,可能干扰脚本或css文件,我需要查看Radiobutton css文件。

试试这个

<div data-toggle="buttons">
      <label class="btn btn-radio">
          @Html.RadioButtonFor(m => m.Claims, "Yes", new { customValue = "Yes" }) Yes
      </label>

      <label class="btn btn-radio">
          @Html.RadioButtonFor(m => m.Claims, "No", new { customValue = "No" }) No
      </label>
</div>
 

脚本

$("input[name='Claims']").on("change", function () {
     var selected = $("input[name='Claims']:checked").attr('customValue')

     if (selected === "Yes") {
        $("#showClaimsHistory").show();
        if (claimCount > 0) {
            $("#showClaimList").show();
        }

     } else {
            $("#showClaimsHistory").hide();
            $("#showClaimList").hide();
     }
});

测试此代码我没有做任何更改,但它工作正常

[data-toggle="buttons"] > .btn input[type="radio"],
[data-toggle="buttons"] > .btn-group > .btn input[type="radio"],
[data-toggle="buttons"] > .btn input[type="checkbox"],
[data-toggle="buttons"] > .btn-group > .btn input[type="checkbox"] {
  position: absolute;
  clip: rect(0, 0, 0, 0);
  pointer-events: none;
}


    .btn {
     display: inline-block;
     margin-bottom: 0;
     font-weight: normal;
     text-align: center;
     vertical-align: middle;
     -ms-touch-action: manipulation;
      touch-action: manipulation;
      cursor: pointer;
      background-image: none;
      border: 1px solid transparent;


white-space: nowrap;
  padding: 7px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.btn:focus,
.btn:active:focus,
.btn.active:focus,
.btn.focus,
.btn:active.focus,
.btn.active.focus {
  outline: 5px auto -webkit-focus-ring-color;
  outline-offset: -2px;
}
.btn:hover,
.btn:focus,
.btn.focus {
  color: #555555;
  text-decoration: none;
}
.btn:active,
.btn.active {
  outline: 0;
  background-image: none;
  -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
.btn.disabled,
.btn[disabled],
fieldset[disabled] .btn {
  cursor: not-allowed;
  opacity: 0.65;
  filter: alpha(opacity=65);
  -webkit-box-shadow: none;
  box-shadow: none;
}
a.btn.disabled,
fieldset[disabled] a.btn {
  pointer-events: none;
     }

.btn-radio {
/*border-radius: 30px;*/
border: 1px solid #dcdcdc;
cursor: pointer;
display: inline-block;
font-weight: 600;
/* padding: .75em 1em; */
text-align: center;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
min-width: 110px;
font-size: 16px;
letter-spacing: 2px;
line-height: 40px;
height: 60px;
text-align: center;
text-transform: none;
padding-left: 4.2em !important;
padding-right: 4.2em !important;
margin-right: 1.2em !important;
 }

.btn-radio:hover {
    background: #1e73e9 !important;
    border: 1px solid #1e73e9 !important;
    color: #fff;
}
<div data-toggle="buttons">
                                    <label class="btn btn-radio">
                                        <input type="radio" id="male" name="Claims" value="Yes">
                                        Yes
                                    </label>
                                    <label class="btn btn-radio">
                                        <input type="radio" id="Famele" name="Claims" value="No">
                                        No
                                    </label>
                                </div>
                                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

                                <script>
                                $("input[name='Claims']").on("change", function () {
    var selected = $("input[name='Claims']:checked").val()
    alert(selected);
    if (this.value === "Yes") {
        

    } else {
        
    }
});
                                </script>

【讨论】:

  • 为单选按钮添加了 CSS 作为对问题的编辑
  • 这可以隐藏/显示所需的内容,但是在提交时它不会绑定到模型。传递给控制器​​的模型仍然保持“是”值。知道为什么会这样吗?
  • 测试这段代码我没有做任何改变,但它工作正常
  • 它应该可以工作,可能是因为程序中的脚本。
  • 把你的代码改成这样:@Html.RadioButton("Claims", "Yes", new { id = "rdbYes"}) @Html.RadioButton("Claims", "No", new { id = "rdbNo"})
猜你喜欢
  • 2013-07-13
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多