繁体   English   中英

JavaScript重定向到下一页错误

[英]JavaScript redirect to next page error

我对javascript完全陌生,我才刚开始学习,就陷入困境。 我正在使用ASP.NET应用程序,并且在我的视图中创建了需要检查是否选中了复选框的JavaScript函数,并且如果选项为“是”或“否”,则需要将其重定向到下一个视图。对于选项“是”,它是完美的,但是当我选择“否”它显示必需的消息,并且在控制台中出现错误

Uncaught TypeError:无法读取HTMLButtonElement.onclick(CreateManually:886)处YesNoChecked(CreateManually:940)的null属性“ checked”

所以到目前为止我的代码

    function YesNoChecked() {
        var Yes = document.getElementById('Yes');
        var No = document.getElementById('No');

        if (document.getElementById('Yes').checked ==true) {
            console.log('Successfull');
        }
        if (document.getElementById('No').checked==false) {
            console.log('Error');
        }
    }

的HTML

<div class="form-group">
  @Html.LabelFor(model => model.Chapel, htmlAttributes: new { @class = "control-label col-md-3" })
  <div class="col-md-9">
  <label class="radio-inline">
    @Html.RadioButtonFor(model => model.Chapel, "Yes", new { @class = "styled", htmlAttributes = new { @checked = "true" } })
    Yes
  </label>
  <label class="radio-inline">
    @Html.RadioButtonFor(model => model.Chapel, "No", new { @class = "styled", htmlAttributes = new { @checked = "true" } })
    No
  </label>
    @Html.ValidationMessageFor(model => model.Chapel, "", new { @class = "text-danger" })
  </div>
</div>

因此,基本上,无论选择“否”还是“是”,都需要重定向到下一个视图,但是当选择“否”时,则需要隐藏名为Chapel的元素。 请帮助大家:/

如果您需要更多代码,请告诉我,我将发布

更新

点击按钮

 <button type="submit" onclick="YesNoChecked()" class="btn btn-success"><i class="icon icon-check position-left"></i> Proceed to Payment</button>

单选按钮是HTML

   <input class="styled" htmlattributes="{ checked = true }" id="Chapel" name="Chapel" type="radio" value="Yes" autocomplete="off">

单选按钮没有HTML

<input checked="checked" class="styled" htmlattributes="{ checked = true }" id="Chapel" name="Chapel" type="radio" value="No" autocomplete="off">

您用于生成复选框的代码是错误的。 至少在asp.net核心中,您应该收到错误“方法'CheckBoxFor'的重载没有3个参数”

如果模型中的Chapel是布尔属性,则将为其创建一个复选框:

@Html.CheckBoxFor(m => m.Chapel, new { id="Yes", @class = "styled", htmlAttributes = new { @checked = "true" } })

如果要单选按钮:

@Html.RadioButtonFor(model => model.Chapel, true, new { id = "Yes", @class = "styled" }) Yes <br/>
@Html.RadioButtonFor(model => model.Chapel, false, new { id = "No", @class = "styled" }) No

这将为您正确创建两个html元素,并使用ID是和否,并让您的js工作:

function YesNoChecked() {
    var Yes = document.getElementById('Yes');
    var No = document.getElementById('No');

    if (Yes.checked ==true) {
        console.log('Successfull');
    }
    if (No.checked==false) {
        console.log('Error');
    }
}

暂无
暂无

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

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