英文:
How to remove ajax selected value from the dropdown
问题
以下是您要翻译的部分:
我有以下的表单字段,在提交后,将通过ajax添加一个值到顶部,这部分正常工作。
<form id="addCarForm">
@csrf
<label for="cars">选择一辆车:</label>
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="添加梅赛德斯" />
<!-- 重置按钮 -->
<button id="resetBtn">重置</button>
</form>
但是重置按钮不会移除所选的值。以下是重置代码:
// 重置按钮点击事件监听器
$("#resetBtn").on("click", function () {
// 将选择元素的值重置为空值
$("#cars").val("");
// 移除通过AJAX添加的选项
$("#cars option[data-ajax]").remove();
});
它创建如下:
<option value="null" selected="">null</option>
但它应该移除如下,但实际上不会执行:
<option value="volvo" selected="">volvo</option>
我已经尝试过如下:
// 将选择元素的值重置为空值
$("#cars").val('');
// 从所有选项中移除selected属性
$("#cars option").prop("selected", false);
还尝试过如下:
// 将选择元素的值重置为空值
$("#cars").val('');
// 获取通过AJAX添加的选项
var ajaxOption = $("#cars option[data-ajax]");
// 如果存在,则移除该选项
if (ajaxOption.length > 0) {
ajaxOption.remove();
}
// 重新添加具有空值的默认选项
$("#cars").prepend('<option value="" data-ajax="">选择一辆车</option>');
但是也没有起作用。我使用了jquery-3.6.0和laravel 10。请帮助我。
英文:
I have the following form field where after submit it will add a value as selected by ajax at the top which works fine.
<form id="addCarForm">
@csrf
<label for="cars">Choose a car:</label>
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="Add Mercedes" />
<!-- Reset button -->
<button id="resetBtn">Reset</button>
</form>
But the reset button does not remove the selected value. Here is the reset code
// Reset button click event listener
$("#resetBtn").on("click", function () {
// Reset the value of the select element to an empty value
$("#cars").val("");
// Remove the option added via AJAX
$("#cars option[data-ajax]").remove();
});
It creates as follows
<option value="null" selected="">null</option>
But it should remove as below but it does not do that
<option value="volvo" selected="">volvo</option>
I have tried as below
// Reset the value of the select element to an empty value
$("#cars").val('');
// Remove the selected attribute from all options
$("#cars option").prop("selected", false);
Also as below
// Reset the value of the select element to an empty value
$("#cars").val('');
// Get the option added via AJAX
var ajaxOption = $("#cars option[data-ajax]");
// Remove the option if it exists
if (ajaxOption.length > 0) {
ajaxOption.remove();
}
// Re-append the default option with an empty value
$("#cars").prepend('<option value="" data-ajax="">Select a car</option>');
But did not work as well. I have used jquery-3.6.0 and laravel 10. Please help me.
答案1
得分: 0
我找到了我的错误。在这里,我在表单内部以及提交按钮之内添加了重置按钮。
<form>
.....
<input type="submit" value="Add Mercedes" />
<button id="resetBtn">Reset</button>
</form>
因此,当我提交时,它会创建一个选定的
所以问题已经解决如下:
<form>
.....
<input type="submit" value="Add Mercedes" />
</form>
<button id="resetBtn">Reset</button>
重置功能如下正常工作:
// 重置按钮点击事件监听器
$("#resetBtn").on("click", function () {
$("#cars :selected").remove();
});
英文:
I found my mistakes. Here I have put reset within the form and with submit.
<form>
.....
<input type="submit" value="Add Mercedes" />
<button id="resetBtn">Reset</button>
</form>
So when I submit it creates a selected <option>. If I click reset button it will delete but at the same time selected <option> will come up again.
So it was solved as below
<form>
.....
<input type="submit" value="Add Mercedes" />
</form>
<button id="resetBtn">Reset</button>
Reset function it works fine as below
// Reset button click event listener
$("#resetBtn").on("click", function () {
$("#cars :selected").remove();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论