如何在 div 显示块后 select 一个单选按钮?
How can I select an radio button after div display block?
这是我的代码:
HTML :
<span id="opc1"> <input type="radio" name="radio_relatorio" id="tipo_relA" value="A"> A </span>
<span id="opc2"> <input type="radio" name="radio_relatorio" id="tipo_relS" value="S"> S </span>
Javascript :
$("select[name=tprel]").change(function() {
document.getElementById("tpbaixa").selectedIndex = 0;
muda_label_datas($(this).val());
if($(this).val() == 18)
{
$("#opc2").css("display","block");
$("#opc1").css("display","none");
$("#tipo_relS").attr('checked', true);
}
else
{
$("#opc2").css("display","none");
$("#opc1").css("display","block");
$("#tipo_relS").attr('checked', false);
} // it's an large function, but that's what matter
});
change函数下有两个Radio,#tipo_relS
和#tiporelA
。 radio #tiporelA
在 onload 函数上被检查。当option值为18时,radio#tiporelA
为displaynone,#tiporelS
为display block。
我需要在显示 #tiporelA
时选中此单选按钮 #tiporelS
none,但似乎没有任何效果。
你可以试试:
$("#tipo_relS").attr('checked', true);
这里的诀窍是您必须取消隐藏单选按钮,单击或选中它,然后在同一个函数中将其全部重新隐藏。用户不会看到更改,但它允许浏览器 select 该值,并且速度足够快,用户看不到它。
正如我在评论中所说,您的描述和代码不匹配!因此,纯粹基于假设我想出了这个。
- 加载时
tipo_relA
被选中,tipo_relS
被隐藏。
- 更改名称为
tprel
的 select 框时,当值为 18 时,隐藏 tipo_relA
并显示 tipo_relS
。
- 已选中显示的单选,未选中隐藏。
演示:
$("select[name=tprel]").change(function() {
$("#opc2, #opc1").addClass("hidden");
$(":radio").prop('checked', false);
if( $(this).val() == 18) {
$("#opc2").removeClass("hidden");
$("#tipo_relS").prop('checked', true);
} else {
$("#opc1").removeClass("hidden");
$("#tipo_relA").prop('checked', true);
}
}).change();
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span id="opc1">
[tipo_relA] <input type="radio" name="radio_relatorio" id="tipo_relA" value="A" />
</span>
<span id="opc2" class="hidden">
[tipo_relS] <input type="radio" name="radio_relatorio" id="tipo_relS" value="S" />
</span>
<select name="tprel">
<option value=""></option>
<option value="18">18</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
这是我的代码:
HTML :
<span id="opc1"> <input type="radio" name="radio_relatorio" id="tipo_relA" value="A"> A </span>
<span id="opc2"> <input type="radio" name="radio_relatorio" id="tipo_relS" value="S"> S </span>
Javascript :
$("select[name=tprel]").change(function() {
document.getElementById("tpbaixa").selectedIndex = 0;
muda_label_datas($(this).val());
if($(this).val() == 18)
{
$("#opc2").css("display","block");
$("#opc1").css("display","none");
$("#tipo_relS").attr('checked', true);
}
else
{
$("#opc2").css("display","none");
$("#opc1").css("display","block");
$("#tipo_relS").attr('checked', false);
} // it's an large function, but that's what matter
});
change函数下有两个Radio,#tipo_relS
和#tiporelA
。 radio #tiporelA
在 onload 函数上被检查。当option值为18时,radio#tiporelA
为displaynone,#tiporelS
为display block。
我需要在显示 #tiporelA
时选中此单选按钮 #tiporelS
none,但似乎没有任何效果。
你可以试试:
$("#tipo_relS").attr('checked', true);
这里的诀窍是您必须取消隐藏单选按钮,单击或选中它,然后在同一个函数中将其全部重新隐藏。用户不会看到更改,但它允许浏览器 select 该值,并且速度足够快,用户看不到它。
正如我在评论中所说,您的描述和代码不匹配!因此,纯粹基于假设我想出了这个。
- 加载时
tipo_relA
被选中,tipo_relS
被隐藏。 - 更改名称为
tprel
的 select 框时,当值为 18 时,隐藏tipo_relA
并显示tipo_relS
。 - 已选中显示的单选,未选中隐藏。
演示:
$("select[name=tprel]").change(function() {
$("#opc2, #opc1").addClass("hidden");
$(":radio").prop('checked', false);
if( $(this).val() == 18) {
$("#opc2").removeClass("hidden");
$("#tipo_relS").prop('checked', true);
} else {
$("#opc1").removeClass("hidden");
$("#tipo_relA").prop('checked', true);
}
}).change();
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span id="opc1">
[tipo_relA] <input type="radio" name="radio_relatorio" id="tipo_relA" value="A" />
</span>
<span id="opc2" class="hidden">
[tipo_relS] <input type="radio" name="radio_relatorio" id="tipo_relS" value="S" />
</span>
<select name="tprel">
<option value=""></option>
<option value="18">18</option>
<option value="2">2</option>
<option value="3">3</option>
</select>