亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何關閉下拉列表所需的消息?

如何關閉下拉列表所需的消息?

C#
狐的傳說 2022-11-13 13:56:37
我有一個包含兩個單選按鈕的表單。一個與下拉菜單相關聯,另一個與文本框相關聯。如果用戶選擇第二個單選按鈕(文本框),我想關閉下拉菜單所需的消息,因為當用戶按下“提交”按鈕時,下拉菜單會顯示一條消息“請選擇一個列表中的項目?!表槺阏f一句,如果我在選擇第二個單選按鈕時禁用下拉菜單,我可以讓它正常工作,但我想知道是否有另一種方法(可能更好),因為當你禁用下拉菜單時,它會變灰。截圖:模型:public class EnrollmentModel : Controller{  ...  public bool PriceOption { get; set; }  [RequiredIf("PriceOption == true")]  public string SelectedRatePlan { get; set; }  public IEnumerable<SelectListItem> RatePlans { get; set; }  [RequiredIf("PriceOption == false")]  public string CustomRate { get; set; }  ...}看法:@using (Html.BeginForm()){  @Html.AntiForgeryToken()  <fieldset>    <div class="form-group">      <div class="row">        @Html.Label("Price Option:")      </div>    </div>    <div class="form-group">      <div class="row">        @Html.RadioButtonFor(x => Model.PriceOption, true, htmlAttributes: new { @id = "comed", @class = "col-md-1" })        @Html.Label("Use price from current rate plan")        &nbsp;        &nbsp;        &nbsp;        @Html.DropDownListFor(x => Model.SelectedRatePlan, new SelectList(Model.RatePlans, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "rateplans", style = "width:100px;", required = "Select Rate Plan" })        @Html.ValidationMessageFor(x => Model.SelectedRatePlan, "", new { @class = "text-danger" })      </div>    </div>    <div class="form-group">      <div class="row">        @Html.RadioButtonFor(x => Model.PriceOption, false, htmlAttributes: new { @id = "custom", @class = "col-md-1" })        @Html.Label("Enter custom price")        &nbsp;        &nbsp;        &nbsp;        @Html.TextBoxFor(x => Model.CustomRate, htmlAttributes: new { @id = "customrate", @class = "form-control", style = "width:100px;" })        @Html.ValidationMessageFor(x => Model.CustomRate, "", new { @class = "text-danger" })      </div>
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

您需要指定該字段在您的模型中是可選的,因為 bool 不能為 null。添加 ”?” 在“bool”之后或使用“Nullable”而不是“bool”

 public bool? PriceOption { get; set; }

該問號表示此屬性可以為空。

在此頁面中,您可以找到如何在單選按鈕上實現更改事件: https ://devdojo.com/tutorials/how-to-detect-radio-box-change-with-jquery

您可以使用以下代碼刪除屬性:

$("#selectElementId").removeAttr("required");


查看完整回答
反對 回復 2022-11-13
  • 1 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號