无法获取值禁用Div标签时

时间:2021-10-05 20:28:53

I have designed a page using MVC Razor. In which I had a div such that is following

我使用MVC Razor设计了一个页面。其中我有一个div正在跟随

 <div class="row" id="DivDisableForView">
            <div class="col-md-4">
                <label for="" class="control-label">
                    Product
                </label>
                <br />
                @Html.DropDownListFor(m => m.ProductSeq, Model.products.ToSelectListItems(x => x.ProductName, x => x.Id.ToString(), "", true, "", "Select"), new { @class = "form-control" }).DisableIf(() => Model.IsReadOnly == true || !Model.IsAdd)
                @Html.HiddenFor(m => m.ProductSeq)
            </div>
            <div class="col-md-4" runat="server">
                <b>Exception Type</b>
                <br />
                @Html.EnumRadioButtonFor(m => m.enumExceptionType, true).DisableIf(() => Model.IsReadOnly == true || !Model.IsAdd)
                @Html.HiddenFor(m => m.enumExceptionType)
            </div>
            <div class="col-md-4" id="divTranMode">
                <b>Transaction Mode</b>
                <br />
                @Html.EnumRadioButtonFor(m => m.enumTransactionMode, true).DisableIf(() => Model.IsReadOnly == true || !Model.IsAdd)
                @Html.HiddenFor(m => m.enumTransactionMode)
            </div>
        </div>

        if (Model.IsAdd)
        {
            <div class="row" id="ViewButton">
                <button id="btnView" name="Submit" class="btn btn-default">
                    View
                </button>
            </div>
        }
    ...
    ...
    ...

If I click the view button,then a grid will load based on the above given field's data and also disabled the fields like following,

如果单击视图按钮,则网格将根据上面给定的字段数据加载,并禁用以下字段,

  $("#DivDisableForView").prop('disabled', true);

After that, When I click the submit button, then the disabled field's data is not present, (i.e) The values are null. So how to avoid it or how to disable a div tag without its value get null or empty.Please anybody help

之后,当我单击提交按钮时,则不存在禁用字段的数据,(即)值为空。那么如何避免它或如何禁用div标签而不使其值为null或为空。请任何人帮忙

3 个解决方案

#1


2  

Html elements with disabled="true" attribute are not going to be sent over in form data. You can just hide the div, and the data will still be sent for processing. Use:

具有disabled =“true”属性的Html元素不会在表单数据中发送。您可以隐藏div,数据仍将被发送以进行处理。使用:

$("#DivDisableForView").hide();

EDIT

编辑

No, I want to show the fields in the screen

不,我想在屏幕上显示字段

In case you want to keep the fields visible, you could try to set them to readonly.

如果您想保持字段可见,您可以尝试将它们设置为只读。

Something like:

就像是:

$('#DivDisableForView input').attr('readonly', 'readonly');
//maybe also set color to gray
$('#DivDisableForView input').css("background-color", "#eee");

Readonly attributes are still going to be sent over in form data.

Readonly属性仍将在表单数据中发送。

EDIT 2

编辑2

Solution for select fields:

选择字段的解决方案:

This JS would solve the select problem:

这个JS可以解决选择问题:

    $('#DivDisableForView input, #DivDisableForView select').attr('readonly', 'readonly');
    //maybe also set color to gray
    $('#DivDisableForView input, #DivDisableForView select').css("background-color", "#eee"); 
    //selects
    $('#DivDisableForView select').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
    $('#DivDisableForView select').attr('onchange', 'this.selectedIndex=this.defaultIndex');

Here is a JSFiddle of it: https://jsfiddle.net/ddan/W4Km8/5340/

这是一个JSFiddle:https://jsfiddle.net/ddan/W4Km8/5340/

Hope it helps.

希望能帮助到你。

EDIT 3

编辑3

For radio and checkbox you can do the following:

对于广播和复选框,您可以执行以下操作:

$('#DivDisableForView :radio, #DivDisableForView :checkbox').click(function()        {
    return false;
});

See example (updated JSFiddle): https://jsfiddle.net/ddan/W4Km8/5341/

参见示例(更新的JSFiddle):https://jsfiddle.net/ddan/W4Km8/5341/

#2


1  

Please hide your div instead of disabling.

请隐藏您的div而不是禁用。

Use this

用这个

$("#DivDisableForView").css('display', 'none');

OR

要么

$("#DivDisableForView").hide();

#3


1  

use .hide() or apply using .css('display','none') , if you want to disable only, first get the value of the div and store it in a variable then disable the div.

使用.hide()或使用.css('display','none'),如果你只想禁用,首先获取div的值并将其存储在变量中然后禁用div。

var divvalue = $("#DivDisableForView").val();
$("#DivDisableForView").prop('disabled', true);

#1


2  

Html elements with disabled="true" attribute are not going to be sent over in form data. You can just hide the div, and the data will still be sent for processing. Use:

具有disabled =“true”属性的Html元素不会在表单数据中发送。您可以隐藏div,数据仍将被发送以进行处理。使用:

$("#DivDisableForView").hide();

EDIT

编辑

No, I want to show the fields in the screen

不,我想在屏幕上显示字段

In case you want to keep the fields visible, you could try to set them to readonly.

如果您想保持字段可见,您可以尝试将它们设置为只读。

Something like:

就像是:

$('#DivDisableForView input').attr('readonly', 'readonly');
//maybe also set color to gray
$('#DivDisableForView input').css("background-color", "#eee");

Readonly attributes are still going to be sent over in form data.

Readonly属性仍将在表单数据中发送。

EDIT 2

编辑2

Solution for select fields:

选择字段的解决方案:

This JS would solve the select problem:

这个JS可以解决选择问题:

    $('#DivDisableForView input, #DivDisableForView select').attr('readonly', 'readonly');
    //maybe also set color to gray
    $('#DivDisableForView input, #DivDisableForView select').css("background-color", "#eee"); 
    //selects
    $('#DivDisableForView select').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
    $('#DivDisableForView select').attr('onchange', 'this.selectedIndex=this.defaultIndex');

Here is a JSFiddle of it: https://jsfiddle.net/ddan/W4Km8/5340/

这是一个JSFiddle:https://jsfiddle.net/ddan/W4Km8/5340/

Hope it helps.

希望能帮助到你。

EDIT 3

编辑3

For radio and checkbox you can do the following:

对于广播和复选框,您可以执行以下操作:

$('#DivDisableForView :radio, #DivDisableForView :checkbox').click(function()        {
    return false;
});

See example (updated JSFiddle): https://jsfiddle.net/ddan/W4Km8/5341/

参见示例(更新的JSFiddle):https://jsfiddle.net/ddan/W4Km8/5341/

#2


1  

Please hide your div instead of disabling.

请隐藏您的div而不是禁用。

Use this

用这个

$("#DivDisableForView").css('display', 'none');

OR

要么

$("#DivDisableForView").hide();

#3


1  

use .hide() or apply using .css('display','none') , if you want to disable only, first get the value of the div and store it in a variable then disable the div.

使用.hide()或使用.css('display','none'),如果你只想禁用,首先获取div的值并将其存储在变量中然后禁用div。

var divvalue = $("#DivDisableForView").val();
$("#DivDisableForView").prop('disabled', true);