checked="checked" value="" > ... isPush 的值将为 0 或 1。因此我的-6ren">
gpt4 book ai didi

php - 如何为已选择的单选按钮编写事件?

转载 作者:行者123 更新时间:2023-12-02 19:12:11 24 4
gpt4 key购买 nike

我的单选按钮代码片段是 -

<input type="radio" name="isPush" id="isPushYes" <? if($isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "Yes"?></input>
<input type="radio" name="isPush" id="isPushNo" <? if(!$isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "No"?></input>
<table id="emailTable"><tr><td>...</td></tr></table>

isPush 的值将为 0 或 1。因此我的单选按钮之一将始终被选中。最初选择任一单选按钮并不在我手中。

现在,对于表格,我想在选择第二个单选按钮时设置 display: none ,并在选择第一个单选按钮时设置 display:visible

那么我应该放置哪个事件?我当然不能放置onclick

最佳答案

您需要检查所选 radio 的 onload 以及 onchange。这意味着如果默认选择,则该表将从一开始就被隐藏。

<强> jsFiddle Demo

function checkRadio()
{
var tbl = document.getElementById("emailTable");

if(document.getElementById("isPushYes").checked)
{
tbl.style.display = "";
}
else
{
tbl.style.display = "none";
}
}

window.onload = function()
{
document.getElementById("isPushYes").onchange = checkRadio;
document.getElementById("isPushNo").onchange = checkRadio;

checkRadio();
}​

另一种选择是没有 onload 事件,您可以使用 PHP 设置显示:

<table id="emailTable" <?php if(!$isPush) echo 'style="display:none"'; ?>><tr><td>...</td></tr></table>

关于php - 如何为已选择的单选按钮编写事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13583300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com