基于单选的表格中的jQuery / Javascript设置输入框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了基于单选的表格中的jQuery / Javascript设置输入框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3142字,纯文字阅读大概需要5分钟。
内容图文
![基于单选的表格中的jQuery / Javascript设置输入框](/upload/InfoBanner/zyjiaocheng/669/25820d31f0cf47c2958af286cb17c2f3.jpg)
我试图根据表格内的单选删除输入框内的值.它是一张桌子的事实正给我造成麻烦.
在我提供的有趣示例中,如果他们说他们不喜欢单选按钮上的炸玉米饼,我想删除他们最喜欢的食物.这是我尝试解决我的问题的尝试之一,但是它没有任何作用.
$(function() {
$
var $row = $(this).closest('tr')
$('.likesTacos').change(function() {
if (!$row.find(".likesTacos[value=['1']").is(":checked")) {
$row.find('.favFood').val("")
} else {
console.log("Good choice in food!")
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Favorite Food</th>
<th>Likes Tacos</th>
</tr>
<tr>
<td>Aaron</td>
<td>Rodgers</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos1" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos1" class="likesTacos" value=0 />No</label>
</div>
</tr>
<tr>
<td>Brett</td>
<td>Favre</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos2" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos2" class="likesTacos" value=0 />No</label>
</div>
</td>
</tr>
<tr>
<td>Bart</td>
<td>Starr</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos3" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos3" class="likesTacos" value=0 />No</label>
</div>
</td>
</tr>
</table>
解决方法:
您可以将其清空,但如果他们改变主意,也可以将其放回原处.
在这里,我将逐步引导您完成代码
// reference to food
let fd = $(this).closest('tr').find('.favFood');
// food value, if blank, get the origial stored
let v = fd.val() == "" ? fd.data('org') : fd.val();
// store our value v
fd.data('org', v);
// blank or not depending on the value
fd.val(($(this).val() == 1) ? v : "");
$(function() {
$('.likesTacos').on('change', function() {
let fd = $(this).closest('tr').find('.favFood');
let v = fd.val() == "" ? fd.data('org') : fd.val();
fd.data('org', v);
fd.val(($(this).val() == 1) ? v : "");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Favorite Food</th>
<th>Likes Tacos</th>
</tr>
<tr>
<td>Aaron</td>
<td>Rodgers</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos1" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos1" class="likesTacos" value=0 />No</label>
</div>
</tr>
<tr>
<td>Brett</td>
<td>Favre</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos2" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos2" class="likesTacos" value=0 />No</label>
</div>
</td>
</tr>
<tr>
<td>Bart</td>
<td>Starr</td>
<td><input textbox class="favFood" value="Tacos"></td>
<td>
<div>
<label><input type="radio" name="tacos3" class="likesTacos" value=1 />Yes</label>
</div>
<div>
<label><input type="radio" name="tacos3" class="likesTacos" value=0 />No</label>
</div>
</td>
</tr>
</table>
内容总结
以上是互联网集市为您收集整理的基于单选的表格中的jQuery / Javascript设置输入框全部内容,希望文章能够帮你解决基于单选的表格中的jQuery / Javascript设置输入框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。