jquery显示/隐藏字段
我在正确隐藏/显示文本字段时遇到问题。这是示例:
我想要的是文本字段仅在选定值时显示等于一并且对其他一切都隐藏。复选框部分工作正常。
从jsfiddle复制的代码:
<html>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title>Test Page</title>
<body>
<table id="testList">
<tbody>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option><input type='text' style="display: none">
</select>
</td>
</tr>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option>
</select><input type='text' style="display: none">
</td>
</tr>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option>
</select> <input type='text' style="display: none">
</td>
</tr>
</tbody>
</table>
<script>
jQuery(function($) {
$("select").change(function() {
var $this = $(this),
$checkbox = $this.parent().prev().find(":checkbox");
if ($this.val() != 0) {
$checkbox.attr("disabled", true);
if($this.val() == 1){
$(":text").show();
}
else{
$(":text").hide();
}
} else {
$checkbox.removeAttr("disabled");
}
});
$(":checkbox").change(function() {
var $this = $(this),
$select = $this.parent().next().find("select");
if ($this.is(":checked")) {
$select.attr("disabled", true);
} else {
$select.removeAttr("disabled");
}
});
});
</script>
</body>
</html>
I am having problems with getting my text field to hide/show properly. here is the example:
What I want is for the text field to only show when the selected value is equal to one and to be hidden for everything else. The checkbox portion is working properly.
Code copied from jsfiddle:
<html>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title>Test Page</title>
<body>
<table id="testList">
<tbody>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option><input type='text' style="display: none">
</select>
</td>
</tr>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option>
</select><input type='text' style="display: none">
</td>
</tr>
<tr>
<td><input type='checkbox'></td>
<td><select>
<option value="0">Volvo</option>
<option value="1">Saab</option>
<option value="2">Mercedes</option>
<option value="3">Audi</option>
</select> <input type='text' style="display: none">
</td>
</tr>
</tbody>
</table>
<script>
jQuery(function($) {
$("select").change(function() {
var $this = $(this),
$checkbox = $this.parent().prev().find(":checkbox");
if ($this.val() != 0) {
$checkbox.attr("disabled", true);
if($this.val() == 1){
$(":text").show();
}
else{
$(":text").hide();
}
} else {
$checkbox.removeAttr("disabled");
}
});
$(":checkbox").change(function() {
var $this = $(this),
$select = $this.parent().next().find("select");
if ($this.is(":checked")) {
$select.attr("disabled", true);
} else {
$select.removeAttr("disabled");
}
});
});
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://jsfiddle.net/vsYB8/20/
而不是使用
$(': text').show()
您需要$(this).siblings(':text').show()
。这将选择相应的文本框,而不是全部。
http://jsfiddle.net/vsYB8/20/
Instead of using
$(':text').show()
you need$(this).siblings(':text').show()
.This will select the corresponding text box and not all of them.