[英]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.