如何使用 htmx 将表单按类型发送到输入框?

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

How can i send form by type into input using htmx?

问题

这是您的代码,已经进行了中文翻译:

<form hx-get="{% url 'search-spec' %}" hx-target="#search-results" hx-trigger="keyup changed delay:150ms">
    <div class="search w-100 mt-4 d-flex justify-content-center">
        <div class="input-group rounded w-75 shadow-4-soft">
            <span class="input-group-text me-2" id="search-addon">
                <i class="fas fa-search"></i>
            </span>
            <input type="search" class="form-control rounded" placeholder="开始输入..." name="q" id="q"/>
        </div>
    </div>
    <div class="search w-100 mt-2 d-flex justify-content-center">
        <span class="input-group-text me-2" id="search-addon">
            <i class="fas fa-sort"></i>
        </span>
        <select name="sort" id="sort" value="0" class="form-select w-50">
            <optgroup label="按发布日期">
                <option value="0">最新发布</option>
                <option value="1" selected>最早发布</option>
            </optgroup>
            <optgroup label="按标志">
                <option value="2">标志 0-9</option>
                <option value="3">标志 9-0</option>
            </optgroup>
            <optgroup label="按名称">
                <option value="4">名称 A-Z</option>
                <option value="5">名称 Z-A</option>
            </optgroup>
        </select>
    </div>
</form>
英文:

I have a search input and sort select. I wrapped it into form. How can i send form by typing into search input (hx-trigger="keyup changed delay:150ms") or selecting another item in select (hx-trigger="changed"). I need to send data from both of this elements.
Here is my code:

&lt;form hx-get=&quot;{% url &#39;search-spec&#39; %}&quot; hx-target=&quot;#search-results&quot; hx-trigger=&quot;keyup delay:150ms changed&quot;&gt;
    &lt;div class=&quot;search w-100 mt-4 d-flex justify-content-center&quot;&gt;
        &lt;div class=&quot;input-group rounded w-75 shadow-4-soft&quot;&gt;
            &lt;span class=&quot;input-group-text me-2&quot; id=&quot;search-addon&quot;&gt;
                &lt;i class=&quot;fas fa-search&quot;&gt;&lt;/i&gt;
            &lt;/span&gt;
            &lt;input type=&quot;search&quot; class=&quot;form-control rounded&quot; placeholder=&quot;Начните набирать...&quot; name=&quot;q&quot; id=&quot;q&quot;/&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;search w-100 mt-2 d-flex justify-content-center&quot;&gt;
        &lt;span class=&quot;input-group-text me-2&quot; id=&quot;search-addon&quot;&gt;
            &lt;i class=&quot;fas fa-sort&quot;&gt;&lt;/i&gt;
        &lt;/span&gt;
        &lt;select name=&quot;sort&quot; id=&quot;sort&quot; value=&quot;0&quot; class=&quot;form-select w-50&quot;&gt;
            &lt;optgroup label=&quot;По дате публикации&quot;&gt;
                &lt;option value=&quot;0&quot;&gt;Последние публикации&lt;/option&gt;
                &lt;option value=&quot;1&quot; selected&gt;Первые публикации&lt;/option&gt;
            &lt;/optgroup&gt;
            &lt;optgroup label=&quot;По обозначению&quot;&gt;
                &lt;option value=&quot;2&quot;&gt;Обозначение 0-9&lt;/option&gt;
                &lt;option value=&quot;3&quot;&gt;Обозначение 9-0&lt;/option&gt;
            &lt;/optgroup&gt;
            &lt;optgroup label=&quot;По наименованию&quot;&gt;
                &lt;option value=&quot;4&quot;&gt;Наименование А-Я&lt;/option&gt;
                &lt;option value=&quot;5&quot;&gt;Наименование Я-А&lt;/option&gt;
            &lt;/optgroup&gt;
        &lt;/select&gt;
    &lt;/div&gt;
&lt;/form&gt;

答案1

得分: 0

你可以在输入和选择中分别定义hx标签,然后在两者中使用

hx-include="closest form"


以便在请求中包括该表单中的所有字段值。

这样也可以让你更好地控制hx-target以及在你单独定义它们时如何触发变化。

参考链接:
https://htmx.org/attributes/hx-include/
英文:

You can define both hx tags seperately in the input and select and then use

hx-include=&quot;closest form&quot;

in both, to include all field values from that form in the request.

This should also give you better control over hx-target and how you trigger changes, when you have them defined separately.

Ref:
https://htmx.org/attributes/hx-include/

huangapple
  • 本文由 发表于 2023年5月17日 15:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269339.html
匿名

发表评论

匿名网友

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

确定