HTMX 和 AlpineJS 在监听选择变更事件时发生冲突。

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

HTMX and AlpineJS conflicting on listening for a select change event

问题

我正在尝试让HTMX和AlpineJS一起在选择元素上工作,我的期望是:

  • 当用户更改选择值时,它调用/v2/Url?param=value&selectId=1

实际发生的情况是:

  • 它调用URL而不附加selectId查询参数:/v2/Url?param=value

我认为最好的代码如下:

<select 
    name="selectId" 
    id="selectId"

    hx-get="/v2/Url?param=value&amp;selectId=1"
    hx-target="#targetId" 

    x-data="{ disabled: false }"
    x-bind:disabled="disabled" 
    x-on:change="disabled = true" 
    x-on:htmx:after-on-load="disabled = false">

    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

<div id="targetId">

</div>

当我删除x-on:change="disabled = true"时,HTMx工作正常,但当我添加它时,似乎HTMx停止监听change事件。

英文:

I'm trying to make HTMX and AlpineJS work together on a select element, what I expect:

  • When user change select value it calls /v2/Url?param=value&amp;selectId=1

What happens:

  • It calls the url without appending selectId query parameter: /v2/Url?param=value

Best code that I can think of:

&lt;select 
    name=&quot;selectId&quot; 
    id=&quot;selectId&quot;

    hx-get=&quot;/v2/Url?param=value&quot;
    hx-target=&quot;#targetId&quot; 

    x-data=&quot;{ disabled: false }&quot;
    x-bind:disabled=&quot;disabled&quot; 
    x-on:change=&quot;disabled = true&quot; 
    x-on:htmx:after-on-load=&quot;disabled = false&quot;&gt;

    &lt;option value=&quot;1&quot;&gt;Item 1&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Item 2&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Item 3&lt;/option&gt;
&lt;/select&gt;


&lt;div id=&quot;targetId&quot;&gt;

&lt;/div&gt;

When I remove the x-on:change=&quot;disabled = true&quot; HTMx works fine, but when I add it, looks like HTMx stop listening for the change event.

答案1

得分: 1

根据@Alejandro的评论,我能够使用alpine的$nextTick函数解决这个问题,最终的代码如下:

<select 
    name="selectId" 
    id="selectId"

    hx-get="/v2/Url?param=value"
    hx-target="#targetId" 

    x-data="{ disabled: false }"
    x-bind:disabled="disabled" 
    x-on:change="$nextTick(() => disabled = true)"
    x-on:htmx:after-on-load="disabled = false">

    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>


<div id="targetId">

</div>
英文:

Following @Alejandro comment I was able to solve this issue using alpine $nextTick function, final code:

&lt;select 
    name=&quot;selectId&quot; 
    id=&quot;selectId&quot;

    hx-get=&quot;/v2/Url?param=value&quot;
    hx-target=&quot;#targetId&quot; 

    x-data=&quot;{ disabled: false }&quot;
    x-bind:disabled=&quot;disabled&quot; 
    x-on:change=&quot;$nextTick(() =&gt; disabled = true)&quot;
    x-on:htmx:after-on-load=&quot;disabled = false&quot;&gt;

    &lt;option value=&quot;1&quot;&gt;Item 1&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Item 2&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Item 3&lt;/option&gt;
&lt;/select&gt;


&lt;div id=&quot;targetId&quot;&gt;

&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年7月20日 20:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729763.html
匿名

发表评论

匿名网友

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

确定