【问题标题】:Not able to set textarea and checkbox to set value in mvc5无法设置 textarea 和复选框以在 mvc5 中设置值
【发布时间】:2023-03-12 06:10:01
【问题描述】:

您好,我正在尝试在 mvc 5 的 javascript + jquery 中为 textarea 和 checkbox 设置我的值,但它不起作用。

我的 javscript 代码是

 document.getElementById("UpdatetxtDescription").value = "abc";       
 document.getElementById("UpdateActive").checked = true;   

我的查看页面代码为

@Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", id = "UpdatetxtDescription" } })
@Html.CheckBoxFor(model => model.Active, new { htmlAttributes = new { @class = "form-control", id = "UpdateActive" } })

在控制台中我可以看到

<input data-val="true" data-val-required="The Active field is required." htmlattributes="{ class = form-control, id = UpdateActive }" id="Active" name="Active" type="checkbox" value="true">

<textarea cols="20" htmlattributes="{ class = form-control, id = UpdatetxtDescription }" id="Description" name="Description" rows="2"></textarea>

我可以使用 id="Description" 和 id="Active" 螺母设置值,因为我有 2 个复选框和 2 个文本区域,我无法使用该 id,因为这两个控件的 id 与 textarea 的描述和 Active 相同复选框

第二个 textarea 和 checkbox 如下所示,我已将其 Id 设置为不同

@Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control", id = "txtDescription", rows = "3" } })
@Html.CheckBoxFor(model => model.Active, new { htmlAttributes = new { @class = "form-control", id = "txtDescription" } })

但在控制台中我可以看到 ID 为

<textarea cols="20" htmlattributes="{ class = form-control, id = txtDescription, rows = 3 }" id="Description" name="Description" rows="2"></textarea>
<input data-val="true" data-val-required="The Active field is required." htmlattributes="{ class = form-control, id = txtDescription }" id="Active" name="Active" type="checkbox" value="true">

【问题讨论】:

  • 您生成表单控件的代码是错误的 - 它的 @Html.TextAreaFor(m =&gt; m.Description, { @class = "form-control", id = "UpdatetxtDescription" })(不是 new { htmlAttributes = { ...),那么您的选择器将正常工作,但您的代码建议您为同一属性生成 2 个控件将无论如何都不能正常工作

标签: javascript jquery asp.net-mvc asp.net-mvc-4 asp.net-mvc-3


【解决方案1】:

您以错误的方式设置 HTML 助手的 htmlAttributes 参数。如果您看到生成的 HTML,您可以看到 idclass 没有被设置。但是一个名为htmlAttributesattribute 本身是这样添加的:

<textarea cols="20" htmlattributes="{ class = form-control, id = UpdatetxtDescription }"></textarea>

所以你应该把它改成:

@Html.TextAreaFor(model => model.Description, new { @class = "form-control", id = "UpdatetxtDescription" })
@Html.CheckBoxFor(model => model.Active, new { @class = "form-control", id = "UpdateActive" })

@Html.TextAreaFor(model => model.Description, new { @class = "form-control", id = "txtDescription", rows = "3" })
@Html.CheckBoxFor(model => model.Active, new { @class = "form-control", id = "txtDescription" })

(PS:如果您要提交此表单,则只有一个 textarea 和 checkbox 将绑定到模型,因为您正在创建多个具有相同 name.attribute 的输入)

【讨论】:

    【解决方案2】:

    id 在同一个文档中应该是唯一的以获得有效结构,如果您不能更改它,那么您可以使用 jQuery 选择器来处理这种重复,例如:

    $('[id="Description"]:eq(0)') //Select the first element with id "Description"
    $('[id="Description"]:eq(1)') //Select the second element with id "Description"
    

    第二个元素input也是一样的:

    $('[id="Active"]:eq(0)') //Select the first element with id "Active"
    $('[id="Active"]:eq(1)') //Select the second element with id "Active"
    

    希望这会有所帮助。

    【讨论】:

    • 如何在这里设置值 $('[id="Description"]:eq(1)').val('abc') 和 $('[id="Active"] :eq(1)').checked(true) 像这样?
    • 如果你能帮助我,我有一个问题。一旦我像这样将复选框的值设置为 true $('[id="Active"]:eq(1)').prop('checked', Active.toLowerCase());然后它永远不会设置为假,一个设置为假它永远不会设置为真。我在 javascript.Jquery 中处理这个。
    • 如果您想将其设置为false,您可以使用$('[id="Active"]:eq(1)').removeAttr('checked')
    • 一旦设置为假,就永远不会设置为真
    • @Xtremcool 有重复的id 是无效的 HTML。你的javascript很好。问题出在cshtml
    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多