0

次のような内容のテキスト ファイルがいくつかあります。

<select name='CitySearch' id='CitySearch'>
   <option value=''>- Select a City -</option>
   <option value=''>-----------</option>
   <option value='Bejuco'>Bejuco</option>
   <option value='Esterillos'>Esterillos</option>
</select>

もちろんそれぞれ内容は異なります。ファイル名に関係なく、example.txt を使用できます

私は地図を持っています。任意の地域をクリックすると、スクリプトはフォーム内に txt を追加する必要があります。これは、クリックしたときの機能です。

// Assigning an action to the click event
$(this).click(function(e) {
    var country_id = $(this).attr('id').replace('area_', '');

    if($("div[id^='area_']").length = 1){
        $("div[id^='area_']").remove(); //remove the last inserted select option
        }
var append_data = '<div id="area_'+country_id+'">**INSERT HTML FROM FILE HERE**</div>';
$("#text_boxes").append(append_data); //append new select options in main div
    });

それが基本的なことであることは知っていますが、jqueryの経験はあまりありません。

4

2 に答える 2

1

.load()を使用してファイルの内容を取得し、一致した要素に挿入できます。

var append_data = '<div id="area_'+country_id+'"></div>';
$("#text_boxes").append(append_data); //append new select options in main div

$("#area_"+country_id).load("path/to/file.html"); // load html file
于 2013-04-25T20:00:05.097 に答える
0

ファイルの URL に対して ajax 呼び出しを実行し、応答を文字列に追加します。

    $(this).click(function(e) {
        var country_id = $(this).attr('id').replace('area_', '');

        if($("div[id^='area_']").length = 1){
            $("div[id^='area_']").remove(); //remove the last inserted select option
            }
          appendNewResult(country_id);
        });
function appendNewResult(country_id){
        $.get(filepath,function(response){
                      var append_data = '<div id="area_'+country_id+'">' + response +'</div>';
    $("#text_boxes").append(append_data); //append new select options in main div

        });
}
于 2013-04-25T20:03:56.627 に答える