如何从下拉菜单中移除ajax选择的值

huangapple go评论103阅读模式
英文:

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.

        &lt;form id=&quot;addCarForm&quot;&gt;
            @csrf
            &lt;label for=&quot;cars&quot;&gt;Choose a car:&lt;/label&gt;
            &lt;select name=&quot;cars&quot; id=&quot;cars&quot;&gt;
                &lt;option value=&quot;volvo&quot;&gt;Volvo&lt;/option&gt;
                &lt;option value=&quot;saab&quot;&gt;Saab&lt;/option&gt;
                &lt;option value=&quot;mercedes&quot;&gt;Mercedes&lt;/option&gt;
                &lt;option value=&quot;audi&quot;&gt;Audi&lt;/option&gt;
            &lt;/select&gt;
            &lt;input type=&quot;submit&quot; value=&quot;Add Mercedes&quot; /&gt;
            &lt;!-- Reset button --&gt;
            &lt;button id=&quot;resetBtn&quot;&gt;Reset&lt;/button&gt;
        &lt;/form&gt;

But the reset button does not remove the selected value. Here is the reset code

// Reset button click event listener
$(&quot;#resetBtn&quot;).on(&quot;click&quot;, function () {
    // Reset the value of the select element to an empty value
    $(&quot;#cars&quot;).val(&quot;&quot;);

    // Remove the option added via AJAX
    $(&quot;#cars option[data-ajax]&quot;).remove();
});

It creates as follows

&lt;option value=&quot;null&quot; selected=&quot;&quot;&gt;null&lt;/option&gt;

But it should remove as below but it does not do that

&lt;option value=&quot;volvo&quot; selected=&quot;&quot;&gt;volvo&lt;/option&gt;

I have tried as below

// Reset the value of the select element to an empty value
$(&quot;#cars&quot;).val(&#39;&#39;);

// Remove the selected attribute from all options
$(&quot;#cars option&quot;).prop(&quot;selected&quot;, false);

Also as below

// Reset the value of the select element to an empty value
$(&quot;#cars&quot;).val(&#39;&#39;);

// Get the option added via AJAX
var ajaxOption = $(&quot;#cars option[data-ajax]&quot;);

// Remove the option if it exists
if (ajaxOption.length &gt; 0) {
	ajaxOption.remove();
}

// Re-append the default option with an empty value
$(&quot;#cars&quot;).prepend(&#39;&lt;option value=&quot;&quot; data-ajax=&quot;&quot;&gt;Select a car&lt;/option&gt;&#39;);

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.

&lt;form&gt;
.....
&lt;input type=&quot;submit&quot; value=&quot;Add Mercedes&quot; /&gt;       
&lt;button id=&quot;resetBtn&quot;&gt;Reset&lt;/button&gt;
&lt;/form&gt;

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

&lt;form&gt;
.....
&lt;input type=&quot;submit&quot; value=&quot;Add Mercedes&quot; /&gt;       
&lt;/form&gt;
&lt;button id=&quot;resetBtn&quot;&gt;Reset&lt;/button&gt;

Reset function it works fine as below

// Reset button click event listener
$(&quot;#resetBtn&quot;).on(&quot;click&quot;, function () {
    $(&quot;#cars :selected&quot;).remove();
});

huangapple
  • 本文由 发表于 2023年7月24日 00:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定