2017-03-18 1 views
3

iframeの制限のためにdiv経由でページを読み込んでいます。私がしたいことは、ページの内容にアクセスし、ドロップダウンの最初の項目を選択することです。ドロップダウンのIDがmyDropdownまたは "ctl00_ctl65_g_549adf60_cb6b_4794_af15_99ce724b040f_FormControl0_V1_I1_D2"のようなものである場合、これを選択してアクセスするにはどうすればいいですか?divを介して読み込まれたページからドロップダウンを選択しています

$(document).ready(function() { 
    $("#load_home").on("click", function() { 
    $("#content").load("https://page.aspx"); 
    }); 
}); 
<div id="topBar"> 
    <a href="#" id="load_home"> Rate!</a> 
</div> 
<div id="content"> 
</div> 
+0

コンソールでクロスオリジンエラーが発生しましたか?具体的にどのWebページですか? – Neil

+0

こんにちは。お待ちください。開始タグの前にjQueryのバージョン1.11.1をロードしてから、古いバージョンのjQueryを再度ロードしていることにご注意ください。 –

+0

これはスニペットエディタのためでした – mplungjan

答えて

1

を呼び出す 'オプション1' に#selを設定します。

var interval; 
    $(document).ready(function() { 
     $("#load_home").on("click", function() { 
      $('.calc-loader').show(); 
      $("#content").load("HtmlPage5.html"); 

      interval = setInterval(function() { 
       console.log($('#content select option').length); 
       if ($('#content select option').length > 2) { 
        clearInterval(interval); 
        $('.calc-loader').hide(); 
        $('#content select option:eq(3)').prop("selected",true); 
        $('#content select').trigger('change'); 
       } 
      }, 1000); 
     }); 
    }); 
+0

なぜ値を値に設定するのですか?選択したものを自分の答えと同じように設定するだけです。 – mplungjan

+0

はい、.prop( "selected"、true)として使用できます。 –

+0

私は知っています。それは私が提案したものだ – mplungjan

0

あなたは.LOADコールバックまたはデリゲートでそれにアクセスする必要があります。

$(function() { 
    $("#load_home").on("click", function() { 
    $("#content").load("https://page.aspx",function() { 
     $("#myDropdown option:eq(2)").prop{"selected",true); 
     // if you have event handlers on the select, you want to trigger them 
     $("#myDropdown").change(); 
    }); 
    }); 
}); 

委任:

$("#content").on("change","#myDropdown",function() { 
    // delegated the change event to the container 
}); 
+0

この方法でアクセスする必要がありますか、またはロード時にDOMの一部になりますか?通常アクセスできますか? – Bango

+0

これは不完全な答えです。 OPは、選択ボックスの指定された「id」および第1のオプションの値を持たない。 –

+0

はい、ほとんど私の唯一のオプション – Vzupo

0

ここでは、純粋なJSを使用してモジュラーfindAndSelect()機能です。パラメータは、選択の文字列ID、および選択するオプションの文字列値です。

は、あなたのHTMLがあると言う:<select id="sel"><option>option1</option></select>、あなたは、このスクリプトを試してみてください、あなたはfindAndSelect('sel','option1');

function findAndSelect (elementId, optionToSelect) { 

    function findRelevantIndex(elementId) { 
    var optionsArr = document.getElementById(elementId).options; 
    var optionsLen = optionsArr.length; 
    for (var i = 0; i < optionsLen; i++) { 
     if (optionsArr[i].text == optionToSelect) { 
     return i; 
     } 
    } 
    console.log('found no matching option as ' + optionToSelect); 
    return 0; 
    } 
    document.getElementById(elementId).selectedIndex = findRelevantIndex(elementId); 
    console.log('new selected index for ' + elementId + ' is ' + document.getElementById(elementId).selectedIndex); 
} 
関連する問題