繁体   English   中英

根据下拉选择清除和启用/禁用字段

[英]clear and enable/disable fields based on drop down selection

我正在尝试清除字段的值,并根据下拉值选择启用/禁用字段。 当前启用/禁用该字段正在工作,但是当要清除该字段时,它不工作。 我已经附上了我的html代码中的代码段,其中应根据状态值来启用其他字段(例如sc,sc凸起等)。 根据状态值更改,prev enabled字段应清除。 甚至应基于它启用提交按钮。 有什么帮助吗?

HTML代码段:

<div class="col-md-6">
   <div class="form-group">
      <label class="control-label col-lg-4">Status:<span class="Imp">*</span></label>
      <div class="col-lg-8">
         @Html.DropDownList("Status", new SelectListItem[] { (new SelectListItem() { Text = "SC", Value = "SC" }), (new SelectListItem() { Text = "PO", Value = "PO" }), (new SelectListItem() { Text = "INV", Value = "INV" }) }, "-- Select Status --", new { @class = "form-control", id = "Status" })
      </div>
   </div>
</div>
<div class="row top-buffer">
   <div class="col-md-6">
      <div class="form-group">
         <label class="control-label col-lg-4">SC Raised:<span class="Imp">*</span></label>
         <div class="col-lg-8">
            <div class='input-group date' id='SCRaisedDatePicker'>
               <input type='text' class="form-control" name="SCRaised" placeholder="MM/DD/YYY" id="SCRaised" />
               <span class="input-group-addon">
               <span class="fa fa-calendar">
               </span>
               </span>
            </div>
         </div>
      </div>
   </div>
   <div class="col-md-6">
      <div class="form-group">
         <label class="control-label col-lg-4">SC:<span class="Imp">*</span></label>
         <div class="col-lg-8">
            @Html.TextBoxFor(model => model.detailsConfig.SC, new { onkeypress = "return isNumberKey(event)", @class = "form-control", id = "SC" })
         </div>
      </div>
   </div>
</div>
<div class="row top-buffer">
   <div class="col-md-6">
      <div class="form-group">
         <label class="control-label col-lg-4">PO#:<span class="Imp">*</span></label>
         <div class="col-lg-8">
            @Html.TextBoxFor(model => model.detailsConfig.PO, new { onkeypress = "return isNumberKey(event)", @class = "form-control", id = "PO" })
         </div>
      </div>
   </div>
   <div class="col-md-6">
      <div class="form-group">
         <label class="control-label col-lg-4">PO Out:<span class="Imp">*</span></label>
         <div class="col-lg-8">
            <div class='input-group date' id='PODatePicker'>
               <input type='text' class="form-control" name="POOut" placeholder="MM/DD/YYY" id="POOut" />
               <span class="input-group-addon">
               <span class="fa fa-calendar">
               </span>
               </span>
            </div>
         </div>
      </div>
   </div>
</div>
<div class="modal-footer">
   <input type="submit" id="btnSubmit" value="Submit" class="btn btn-lg btn-success" />
</div>

Java脚本代码:

$('#Status').change(function() {

    switch ($(this).find('option:selected').text()) {

        case '-- Select Status --':
            $('#SCRaised').prop('disabled', true);
            $('#SC').prop('disabled', true);
            $('#PO').prop('disabled', true);
            $('#POOut').prop('disabled', true);

            break;

        case 'SC':
            $('#SCRaised').prop('disabled', false);
            $('#SC').prop('disabled', false);

            document.getElementById("#PO").value = "";
            document.getElementById("#POOut").value = "";



            $('#PO').prop('disabled', true);
            $('#POOut').prop('disabled', true);

            if ($('#SCRaised').val().length > 0 && $('#SC').val().length > 0) {
                $("input[type=submit]").prop("disabled", false);
            }

            break;

        case 'PO':
            $('#PO').prop('disabled', false);
            $('#POOut').prop('disabled', false);
            $('#SCRaised').val() = "";
            $('#SC').val() = "";


            $('#SCRaised').prop('disabled', true);
            $('#SC').prop('disabled', true);
            $('#ItemArrival').prop('disabled', true);
            if ($('#PO').val().length > 0 &&
                $('#POOut').val().length > 0)

            {
                $("input[type=submit]").prop("disabled", false);
            }
            break;


    }

});

我在您的代码段中注意到的一件事是您使用了不正确的getElementById

document.getElementById("#PO").value = "";

作为参数,您应提供不带#的id。

要从输入中删除值,请使用jQuery val()函数传递一个空字符串作为aprama

$('#SCRaised').val('');

给您一个解决方案

 $('#Status').change(function () { switch($(this).find('option:selected').text()){ case '-- Select Status --': $('#SCRaised, #SC, #PO, #POOut').prop('disabled',true); break; case 'SC' : $('#SCRaised, #SC').prop('disabled', false); $("#PO, #POOut").val(''); $('#PO, #POOut').prop('disabled', true); if ($('#SCRaised').val().length > 0 && $('#SC').val().length > 0) { $("input[type=submit]").prop("disabled", false); } break; case 'PO' : $('#PO, #POOut').prop('disabled', false); $('#SCRaised, #SC').val(''); $('#SCRaised, #SC, #ItemArrival').prop('disabled', true); if( $('#PO').val().length > 0 && $('#POOut').val().length > 0){ $("input[type=submit]").prop("disabled", false); } break; } }); 

希望这将帮助您解决问题并减少代码行数。

意图使用更改-$('#Status')。change(function()用于- $('#Status').on('change',function() {})并用于清除值

$('#PO').val("") 

而不是document.getElementById(“#PO”)。value =“”; 如mic4ael先前所述,无需在JavaScript代码中使用选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM