JS:根据单选按钮问题显示/隐藏文本字段 [英] JS: Show/hide text field depending on radio button issue
问题描述
如果从单选按钮中选择了"1",我有一个JS显示测试字段.如果选择"0",则隐藏.当文本字段从一开始就隐藏时,它可以工作,但是如果我希望该文本字段在数据库中的值为"1",则默认情况下该文本字段不可见.
I have a JS that shows a test field if "1" is selected from a radio button. And hides if "0" is selected. It's working while the text field is hidden from the beginning but not if I want the text field to be visible as default if the value is "1" from the database.
JS
$(document).ready(function() {
$("#send_to_yes").hide();
$("input:radio[name=\'article\']").change(function() {
if(this.value == \'1\' && this.checked){
$("#send_to_yes").show();
}
else {
$("#send_to_yes").hide();
}
});
});
HTML
Yes <input type="radio" name="article" value="1">
No <input type="radio" name="article" value="0">
<div id="send_to_yes">
<b>Number</b> <br><input type="text" name="number"><br><br>
</div>
CSS
#send_to_yes {
display: none;
}
"1"和"0"来自数据库.使用此代码,我需要按是",然后出现文本字段.即使选中是",我也需要按一下.如果希望将"1"(是)选中为默认值,我希望它是可见的.
The "1" and "0" comes from a database. With this code I need to press "Yes" and then the text field comes up. Even if "Yes" is checked I need to press it. I want it to be visible if "1" (Yes) is checked as default.
推荐答案
我认为您想要此
HTML
Yes <input type="radio" name="article" value="1" checked='checked'>
No <input type="radio" name="article" value="0">
<div id="send_to_yes">
<b>Number</b> <br><input type="text" name="number"><br><br>
</div>
Js
$("input[type=radio]").change(function() {
if($(this).prop('value') == '1'){
$("#send_to_yes").show();
}
else {
$("#send_to_yes").hide();
}
});
这篇关于JS:根据单选按钮问题显示/隐藏文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!