簡體   English   中英

需要有關jQuery load()的幫助

[英]need help with jQuery load()

我有一個下拉選擇器,需要根據選擇加載表單的其他部分。 到目前為止,這是我得到的:

$('#mySelector').change(function(){
    var $selectForm = '#' + $(this).val();
    $('#fLoad').load('formParts.html ' + $selectForm );
});

<select id="mySelector">
   <option value="o1">Car details
   <option value="o2">Boat details
   <option value="o3">Train details
</select>

我將表單的一部分存儲到外部HTML formParts.html中

<div id="o1">
    <label for="f1_1">Car speed</label>
    <input id="f1_1" type="text">
    <br>
    <label for="f1_2">Car color</label>
    <input id="f1_2" type="text">
</div>

<div id="o2">
    <label for="f2_1">Boat size</label>
    <input id="f2_1" type="text">
    <br>
    <label for="f2_2">Boat weight</label>
    <input id="f2_2" type="text">
</div>

<div id="o3">
    <label for="f3_1">Train length</label>
    <input id="f3_1" type="text">
    <br>
    <label for="f3_2">Train cargo</label>
    <input id="f3_2" type="text">
</div>

不管我選擇什么,它似乎都加載零件,但僅加載第一個div。 我想念什么?

這是工作代碼: 您的選項標簽格式不正確

<select id="mySelector">
        <option value="0">Select</option>
        <option value="o1">Car details</option>
        <option value="o2">Boat details</option>
        <option value="o3">Train details</option>
    </select> 

formParts.html :您的html標記格式不正確。

<div id="o1">
        <label for="f1_1">Car speed</label>
        <input id="f1_1" type="text" />
        <br />
        <label for="f1_2">Car color</label>
        <input id="f1_2" type="text" />
    </div>

    <div id="o2">
        <label for="f2_1">Boat size</label>
        <input id="f2_1" type="text" />
        <br />
        <label for="f2_2">Boat weight</label>
        <input id="f2_2" type="text" />
    </div>

    <div id="o3">
        <label for="f3_1">Train length</label>
        <input id="f3_1" type="text" />
        <br />
        <label for="f3_2">Train cargo</label>
        <input id="f3_2" type="text" />
    </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM