[英]How to send only inputs with value?
I have a form:我有一个表格:
<form id="carForm">
<div class="form-field">
<label for="">Year:</label><br>
<input id="year" type="number" placeholder="1990" required>
</div>
<div class="form-field">
<label for="">Mileage(km):</label><br>
<input id="mileage" type="number" placeholder="30000" required>
</div>
<div class="form-field">
<label for="">Brand:</label><br>
<input id="brand" type="text" placeholder="Mercedes" maxlength="20" required>
</div>
<div class="form-field">
<label for="">Maintenance:</label><br>
<input id="maintenance" type="number" placeholder="0000" >
</div>
<div class="form-field">
<label for="">Cooling system:</label><br>
<input id="cooling" type="number" placeholder="0000" >
</div>
<div class="form-field">
<label for="">Total amount:</label><br>
<input id="total" type="number" placeholder="0000" disabled>
</div>
<div class="submit-btn">
<input type="submit" value="Add">
</div>
</form>
And a sending method:和一个发送方法:
let dataToSend = {
repair: {
year: yearField.value,
total_amount: totalField.value,
mileage: mileageField.value,
brand: brandField.value,
items: {
maintenance: maintenanceField.value,
cooling_system: coolingField.value,
}
axios({
method: "post",
url: 'url',
data: {
dataToSend
},
How to send only fields containing the value?如何仅发送包含该值的字段? For example: Cooling system input value is 1.000, and maintenance input value is 0. How to send only that one (cooling system) field?
例如:冷却系统输入值为1.000,维护输入值为0。如何只发送那个(冷却系统)字段?
In ES6, you can do在 ES6 中,你可以做
...(condition && {key: value})
Taking your example:以你为例:
const coolingField = {value: 1};
const maintenanceField = {value: 0};
let dataToSend = {
repair: {
items: {
...(coolingField.value > 0 && { maintenance: coolingField.value }),
...(maintenanceField.value > 0 && { cooling_system: maintenanceField.value })
}
}
};
Here is a way to do it.这是一种方法。
let dataToSend = { repair: { year: 15, total_amount: '', mileage: 232, brand: 'asa', items: { maintenance: '', cooling_system: 12, } } } // Remove empty key values const clearEmpties = (obj) => { for (var propName in obj) { if (typeof obj[propName] == "object") clearEmpties(obj[propName]) if (obj[propName] === null || obj[propName] === undefined || obj[propName] ==='') delete obj[propName]; } return obj } console.log(clearEmpties(dataToSend)) /* axios({ method: "post", url: 'url', data: { clearEmpties(dataToSend) }) */
you can use conditions like你可以使用类似的条件
if(maintenanceField.value > 0 && coolingFieldValue <= 0 ){
items: {
maintenance: maintenanceField.value
}
}
elseif(maintenanceField.value <= 0 && coolingFieldValue > 0){
items: {
cooling_system: coolingField.value
}
else{
items: {
maintenance: maintenanceField.value,
cooling_system: coolingField.value,
}
}
} }
To send the inputs with the value you can just check if the input fields is null or not by using conditions like要发送带有值的输入,您可以使用以下条件检查输入字段是否为 null
items: {
...(coolingField.value > 0 && { maintenance: coolingField.value }),
...(maintenanceField.value > 0 && { cooling_system: maintenanceField.value })
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.