在此示例中,我们展示了如何使一个 select 中的值取决于另一个 select 中选择的值。
¥In this example we show how to make the values in one select depend on the value selected in another select.
首先,我们从 make 选择的默认值开始:奥迪。我们为此品牌渲染 model 选择。然后我们让 make 选择触发 GET 到 /models 以检索模型选项并定位 models 选择。
¥To begin we start with a default value for the make select: Audi. We render the model select for this make. We
then have the make select trigger a GET to /models to retrieve the models options and target the models select.
这是代码:
¥Here is the code:
<div>
<label >Make</label>
<select name="make" hx-get="/models" hx-target="#models" hx-indicator=".htmx-indicator">
<option value="audi">Audi</option>
<option value="toyota">Toyota</option>
<option value="bmw">BMW</option>
</select>
</div>
<div>
<label>Model</label>
<select id="models" name="model">
<option value="a1">A1</option>
...
</select>
<img class="htmx-indicator" width="20" src="/img/bars.svg">
</div>
当向 /models 端点发出请求时,我们会返回该模型:
¥When a request is made to the /models end point, we return the models for that make:
<option value='325i'>325i</option>
<option value='325ix'>325ix</option>
<option value='X5'>X5</option>
并且它们在 model 选择中可用。
¥And they become available in the model select.