javascript - 如何在回调时使用 AJAX 请求选择单选按钮值和文本输入?
问题描述
表单数据正确填写在所有输入文本字段中。但是对于单选按钮,除了元素的值发生变化外,什么都没有发生,但没有视觉效果。我知道它是因为基于类名,它会改变值。但是我如何区分现场输入和无线电输入?谢谢!
$(function() {
$(".task-listing").click(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:8090/HELPERSITE/src/php/listing-info-get.php',
dataType: 'json',
data: 'pid=' + $(this).attr("id"),
success: function(response) {
console.log(response);
Object.keys(response[0]).map(function(k) {
var el = $('.' + k);
if (el.prop('type') == 'radio') {
el.filter(function(i, button) {
return button.value == response[0][k];
}).prop('checked', true);
} else {
$('.' + k).val(response[0][k]);
}
})
}
});
});
});
解决方案
如果类型为 ,则需要设置checked
相应按钮的属性radio
。
$(function() {
$(".task-listing").click(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:8090/HELPERSITE/src/php/listing-info-get.php',
dataType: 'json',
data: 'pid=' + $(this).attr("id"),
success: function(response) {
console.log(response);
Object.keys(response[0]).map(function(k) {
var el = $('.' + k);
if (el.prop('type') == 'radio') {
el.filter(function(i, button) {
return button.value == response[0][k];
}).prop('checked', true);
} else {
$('.' + k).val(response[0][k]);
}
if (response.negotiate == 'approve') {});
}
})
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="task-title-reg" name="task-title-reg" value="">
<textarea class="task-description-reg" name="task-description-reg" rows="4" cols="50" value=""></textarea>
<label for="local-reg">Local</label>
<input type="radio" name="task-location-reg" value="Local">
<label for="remote-reg">Remote</label>
<input type="radio" class="task-location-reg" name="task-location-reg" value="Remote">
<label for="approve-reg">Approve</label>
<input type="radio" class="task-negotiations-reg" name="negotiate" value="Approve">
<label for="deny-reg">Deny</label>
<input type="radio" class="task-negotiations-reg" name="negotiate" value="Deny">
推荐阅读
- css - 引导表,数据过滤器被 css 定位为绝对
- linux - 使用 sed 在 Docker 中插入第二行(在 shebang 下)
- apache-dubbo - 为什么 dubbo 提供商总是注册到错误的地址?
- java - “如何修复'NoClassDefFoundError:无法在 Android Robolectric 中初始化类 com.google.android.gms.common.GooglePlayServicesUtil' 错误”
- java - 如何处理没有传出端点的 USB-HID 设备?
- css - 如何在 Safari 和 Chrome 中居中占位符文本?
- python - 如何输出到自定义文件描述符?
- sql - SQL Server 如何根据依赖表正确创建视图?
- python - 字典可以用作 matplotlib.pyplot 的完整输入吗?
- firebase - 如何在颤振中从 FirebaseAuth 电话验证方法返回变量?