页面加载后将复选框设置为 JavaScript Checked
Set Checkbox to Checked with JavaScript after page Load
在页面加载后尝试将我页面上的复选框元素设置为选中时,我遇到了一个小挑战:
<input type="checkbox" id="1403317">
挑战:
1. 这个 <input>
只能通过其 id 调用,因为没有 name 属性集。
2. 执行此操作的 JS 代码不能放在 <head></head>
标记内 - 我无权访问此用例中的那部分代码,因此它必须在 <body></body>
中的某个地方工作。
这是我到目前为止(在结束 </body>
标记之前)尝试执行此操作的方法,但没有任何效果。我的语法有问题吗?
<script type="text/javascript">
window.onload = function() {
document.getElementById("1403317").checked = true;
}
</script>
这应该可以满足您的需求。它在代码段中正常工作。
window.onload = onPageLoad();
function onPageLoad() {
document.getElementById("1403317").checked = true;
}
<input type="checkbox" id="1403317">
也许试试这个?
$(document).ready(function() {
$('#1403317').attr('checked', true)
};
在页面加载后尝试将我页面上的复选框元素设置为选中时,我遇到了一个小挑战:
<input type="checkbox" id="1403317">
挑战:
1. 这个 <input>
只能通过其 id 调用,因为没有 name 属性集。
2. 执行此操作的 JS 代码不能放在 <head></head>
标记内 - 我无权访问此用例中的那部分代码,因此它必须在 <body></body>
中的某个地方工作。
这是我到目前为止(在结束 </body>
标记之前)尝试执行此操作的方法,但没有任何效果。我的语法有问题吗?
<script type="text/javascript">
window.onload = function() {
document.getElementById("1403317").checked = true;
}
</script>
这应该可以满足您的需求。它在代码段中正常工作。
window.onload = onPageLoad();
function onPageLoad() {
document.getElementById("1403317").checked = true;
}
<input type="checkbox" id="1403317">
也许试试这个?
$(document).ready(function() {
$('#1403317').attr('checked', true)
};