我有一个带有文本输入和一个单选按钮对的表单,用于选择是/否。为了简单起见,单选按钮单击事件检查该值,如果是,则显示输入文本字段。如果否,则隐藏输入字段。我还检查文档准备就绪的初始状态,并显示/隐藏输入文本字段。
[我发现单击No会导致使用jQuery .hide()方法隐藏输入。但是,当我选择“是”时,生成的.show()方法调用不会显示输入。如果我将收音机设置为“是”,然后刷新页面,则输入显示就很好。
Firebug不显示输入标签。就像单击“否”单选会从DOM中删除输入一样。
这是JS代码示例:
$(document).ready(function() {
if ($('#cost_sharing_yes').attr('checked') == 'checked') {
$('input#Institutional_CS_TP').show();
} else {
$('input#Institutional_CS_TP').hide();
}
$('#cost_sharing_yes').click(function() {
$('input[id="Institutional_CS_TP"]').show();
});
$('#cost_sharing_no').click(function() {
$('input#Institutional_CS_TP').fadeOut("fast");
});
}
您缺少)
来关闭准备就绪功能:
$(document).ready(function() {
} // <--
要获得输入的checked
属性,应该使用prop
方法而不是attr
。
$(document).ready(function() {
var isChecked = $('#cost_sharing_yes').prop('checked');
$('#Institutional_CS_TP').toggle(isChecked);
// ..
})
我想出了我的问题。这是一个自发的编码问题。为了使示例简单,我删除了我认为与该问题没有任何关系的另一个函数调用。我错了。在该函数中,我有
$('td#Institutional_CS_TP).text('$0');
$('input[name="Institutional_CS_TP"]').val('0.00');
这将仅显示td值,而不显示同一td中的输入。我的td和td内的input标签都具有相同的ID值...不是一个好主意。
html代码
<div id="myRadioGroup">
Value Based<input type="radio" name="cars" value="2" />
Percent Based<input type="radio" name="cars" value="3" />
<br>
<div id="Cars2" class="desc" style="display: none;">
<br>
<label for="txtPassportNumber">Commission Value</label>
<input type="text" id="txtPassportNumber" class="form-control" name="commision_value" />
</div>
<div id="Cars3" class="desc" style="display: none;">
<br>
<label for="txtPassportNumber">Commission Percent</label>
<input type="text" id="txtPassportNumber" class="form-control" name="commision_percent" />
</div>
</div>
Jquery代码
function myFunction() {
var x = document.getElementById("myInput");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
function myFunction1() {
var y = document.getElementById("myInput1");
if (y.type === "password") {
y.type = "text";
} else {
y.type = "password";
}
}