Javascript 在html表中为表单开头的特定行设置只读值

Javascript 在html表中为表单开头的特定行设置只读值,javascript,c#,jquery,asp.net-core,Javascript,C#,Jquery,Asp.net Core,我尝试为特定行设置只读字段,其中选择框中设置了“sth” 该代码在页面加载/表单开始时执行 我试过: .js文件 $(document).ready(function () { $(".ecp-row").each(function () { var start = $(this).find(".start"); var end = $(this).find(".end"

我尝试为特定行设置只读字段,其中选择框中设置了“sth”

该代码在页面加载/表单开始时执行

我试过:

.js文件

 $(document).ready(function () {


      $(".ecp-row").each(function () {

            var start = $(this).find(".start");
            var end = $(this).find(".end");
            var hours = $(this).find(".gethours");
            var selectboxlist = $(this).find(".selectboxlist").eq(0).val();

            if (selectboxlist === "sth") {



                alert("test");  // it works

                start.readOnly = true;  // it doesnt work
                end.readOnly = true;  // it doesnt work
                hours.readOnly = true;   // it doesnt work
                
                
            }
        });

  });
html

@for(int nr_rows=0;nr_rows<@ViewBag.sselse;nr_rows++)
{
@Html.TextBoxFor(m=>m.Model7[nr_行].a,新的{@class=“start”})
@Html.TextBoxFor(m=>m.Model7[nr_行].b,新的{@class=“end”})
@Html.TextBoxFor(m=>m.Model7[nr_行].c,新{@class=“gethours”})
@Html.DropDownListFor(m=>m.Model7[nr_行].sth,新选择列表(Enum.GetValues(typeof(sth)),“”,新{@class=“selectboxlist”})
}

如评论中所述,您的问题是您试图在
jquery
对象上执行javascript代码

您可以使用
start[0]。readOnly=true
start.attr(“只读”、“真”)

演示

$(文档).ready(函数(){
$(“.ecp行”)。每个(函数(){
var start=$(this.find(“.start”);
警报(“测试”);//它可以工作
//开始[0]。只读=true;
start.attr(“只读”、“真”);
});
});

您正在将
readonly
属性设置为
true
错误,请尝试jquery
attr()
方法

 var start = $(this).find(".start");

 start.attr("readOnly","true");// set readonly to true


如果要更改为只读,可以使用
prop
attr

道具:

属性:

这是他们之间的区别

演示:

@for(int nr_rows=0;nr_rows<@ViewBag.sselse;nr_rows++)
{
start@Html.TextBoxFor(m=>m.Model7[nr_行].a,新的{@class=“start”})
end@Html.TextBoxFor(m=>m.Model7[nr_行].b,新{@class=“end”})
gethours@Html.TextBoxFor(m=>m.Model7[nr_行].c,新{@class=“gethours”})
}
@节脚本{
$(函数(){
var start=$(this.find(“.start”);
start.prop(“只读”,true);
var gethours=$(this.find(“.gethours”);
gethours.attr(“只读”,true);
})
}
结果:

start
和本例中的其他变量是一个jquery对象,并且
start.readOnly=true
是一种javascript方式来设置只读,因此可以使用
start[0]。readOnly=true
start.attr(“readOnly”,“true”)
此外,您的
for
循环中的一个注释
td3
和其他id将被复制,并且id应始终保持唯一。
 var start = $(this).find(".start");

 start.attr("readOnly","true");// set readonly to true

var start = $(this).find(".start");
            start.prop("readonly", true);
var gethours = $(this).find(".gethours");
            gethours.attr("readonly", true);
    @for (int nr_rows = 0; nr_rows < @ViewBag.sthelse; nr_rows++)
{
    <tr class="ecp-row">
       
        start<td id="td3">@Html.TextBoxFor(m => m.Model7[nr_rows].a, new { @class = "start" })</td>
        end<td id="td4">@Html.TextBoxFor(m => m.Model7[nr_rows].b, new { @class = "end" })</td>
        gethours<td id="td5">@Html.TextBoxFor(m => m.Model7[nr_rows].c, new { @class = "gethours" })</td>
    </tr>
}
@section scripts{ 
    <script>
        $(function () {
            var start = $(this).find(".start");
            start.prop("readonly", true);
            var gethours = $(this).find(".gethours");
            gethours.attr("readonly", true);
        })
    </script>
}