简体   繁体   English

选中复选框时,Asp.net / Javascript删除输入字段的值

[英]Asp.net / Javascript remove value of input field when checkbox is checked

right now I have a javascript that disables from inputs fields for me. 现在我有一个javascript,它为我禁用了输入字段。 And this is the script that is doing that. 这就是执行此操作的脚本。

    $(document).ready(function() {
    $('#check').click(function(){
        if($(this).prop('checked') === true){
            $('#startTime').prop("disabled","disabled");
            $('#endTime').prop("disabled","disabled");
            $('#breakTime').prop("disabled","disabled");
            $('#workedHours').prop("disabled","disabled");
            $('#addMoreProjects').attr("disabled", "disabled");
            $('#projectName').attr("disabled", "disabled");
            $('#description_1').attr("disabled", "disabled");
            $('#hours_1').attr("disabled", "disabled");
        }
        else {
            $('#startTime').removeAttr('disabled');
            $('#endTime').removeAttr('disabled');
            $('#breakTime').removeAttr('disabled');
            $('#workedHours').removeAttr('disabled');
            $('#addMoreProjects').removeAttr('disabled');
            $('#projectName').removeAttr('disabled');
            $('#description_1').removeAttr('disabled');
            $('#hours_1').removeAttr('disabled');
        }
    });
});

But by default my Input fields has values in them, and when I disble the input fields the values are still there. 但是默认情况下,我的输入字段中包含值,而当我取消输入字段时,值仍然存在。 What I would like to do is when I check the textbox, it will disable the Input field, and remove the value that is inside the box. 我想做的是,当我选中文本框时,它将禁用“输入”字段,并删除框内的值。 And when I uncheck it I want that value to be added back. 当我取消选中它时,我希望将该值添加回去。

This is my view: 这是我的看法:

<div class="portlet-body form">
    <div class="form-group">
        <label class="col-md-5">Ingen tid att rapportera</label>
        @Html.CheckBoxFor(model => model.NoTimeToReport, new { @id = "check" })
    </div>
    @if (Model.ReportId.HasValue)
    {
        <div class="form-group">
            <label class="col-md-4 control-label">Redigera datum:</label>
            <div class="col-md-5">
                @Html.TextBox("date", Model.Date.ToShortDateString(), new {@class = "form-control", @readonly = "true"})
            </div>
        </div>
    }
    <div class="form-group">
        <label class="col-md-4 control-label">Start tid:</label>
        <div class="col-md-5">
            @Html.TextBox("startTime", Model.Times.StartTime, new {@class = "form-control timepicker timepicker-24"})
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4 control-label">Slut tid:</label>
        <div class="col-md-5">
            @Html.TextBox("endTime", Model.Times.EndTime, new {@class = "form-control timepicker timepicker-24"})
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4 control-label">Rast Längd:</label>
        <div class="col-md-5">
            @Html.TextBox("breakTime", Model.Times.BreakTime, new {@class = "form-control timepicker timepicker-24"})
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4 control-label">Tid jobbad:</label>
        <div class="col-md-5">
            @Html.TextBox("workedHours", Model.Times.WorkedHours, new {@class = "form-control timepicker timepicker-24"})
        </div>
    </div>
</div>

try this:- 尝试这个:-

var startTime = $('#startTime');
var endTime = $('#endTime')
var breakTime = $('#breakTime');
var workedHours = $('#workedHours');
var addMoreProjects = $('#addMoreProjects');
var projectName = $('#projectName');
var description_1 = $('#description_1');
var hours_1 = $('#hours_1');

if ($(this).prop('checked') === true) {
    startTime.attr('disabled', 'disabled').attr('data-value', startTime.val()).val('');
                endTime.attr('disabled', 'disabled').attr('data-value', endTime.val()).val('');
                breakTime.attr('disabled', 'disabled').attr('data-value', breakTime.val()).val('');
                workedHours.attr('disabled', 'disabled').attr('data-value', workedHours.val()).val('');
                addMoreProjects.attr('disabled', 'disabled').attr('data-value', addMoreProjects.val()).val('');
                projectName.attr('disabled', 'disabled').attr('data-value', projectName.val()).val('');
                hours_1.attr('disabled', 'disabled').attr('data-value', hours_1.val()).val('');
                description_1.attr('disabled', 'disabled').attr('data-value', description_1.val()).val('');
}
else {   
  startTime.removeAttr('disabled').val(startTime.attr('data-value'));
  endTime.removeAttr('disabled').val(endTime.attr('data-value'));
  breakTime.removeAttr('disabled').val(breakTime.attr('data-value'));
  workedHours.removeAttr('disabled').val(workedHours.attr('data-value'));
 addMoreProjects.removeAttr('disabled').val(addMoreProjects.attr('data-value'));
 projectName.removeAttr('disabled').val(projectName.attr('data-value'));
 hours_1.removeAttr('disabled').val(hours_1.attr('data-value'));
 description_1.removeAttr('disabled').val(description_1.attr('data-value'));
}

The thing is that you should do some extra javascript logic.You could keep your data hidden in some data-value attribute. 问题是您应该执行一些额外的javascript逻辑。您可以将数据隐藏在某些data-value属性中。 Here you have an example for a single element. 在这里,您有一个单个元素的示例。

The trick to preserve the logic for the modified text is to add a change event for each of your textboxes like this: 保留修改后的文本逻辑的技巧是为每个文本框添加一个change事件,如下所示:

$( "#someTextbox" ).change(function() {
  $("#someTextbox").attr('data-value', $(this).val());
});

In order to use this in your situation try to edit the render of textbox similar to (basically i have added the data_value for your textbox, note that you need to wirte it with "_", mvc will take care of the correct formatting): 为了在您的情况下使用此功能,请尝试编辑类似于以下内容的文本框的呈现方式(基本上,我已为文本框添加了data_value,请注意,您需要使用“ _”来填充它,mvc将处理正确的格式):

 @Html.TextBox("startTime", Model.Times.StartTime, new {@class = "form-control timepicker timepicker-24", data_value = Model.Times.StartTime.ToString()})

As you are storing you value in "startTime" textbox, what you can do is just use hiddenfield and store the same value in it and then you can change your code like below. 当您将值存储在“ startTime”文本框中时,您可以做的就是使用hiddenfield并将相同的值存储在其中,然后可以像下面那样更改代码。

In Else Part: 在其他部分:

$('#startTime').val('');
$('#startTime').removeAttr('disabled');

In If Part: 在如果部分中:

$('#startTime').val($('#your date time hidden field').val());

Let me know if it works for you. 请让我知道这对你有没有用。

There is simple example in javascript : javascript有一个简单的示例:

 function chk(ele) { var n1=document.getElementById('txtName'); var n2=document.getElementById('txtName2'); if(ele.checked) { n1.disabled=false;n2.disabled=false; n1.value=n1.getAttribute('data-value'); n2.value=n2.getAttribute('data-value'); n1.focus(); } else { n1.setAttribute('data-value',n1.value); n2.setAttribute('data-value',n2.value); n1.value='';n2.value=''; n1.disabled=true;n2.disabled=true; } } 
 <input type="checkbox" onclick="chk(this);" checked />Check me <br><br> First name : <input type="text" id="txtName" value="" data-value="" /> <br><br> Last name : <input type="text" id="txtName2" value="" data-value="" /> 

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

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