2016-07-12 14 views
2

私は注入されたjavascriptを使ってwebviewでいくつかのアクションを実行するiOSアプリケーションで作業しています。私はルータのWebインターフェイス上の送信ボタンを押してしようとしています(私はあなたにURLを与えることはできません)。私は他の多くの提案されたソリューションをオンラインで試みましたが、何も機能していません。私がしなければならないことの1つは、タイプ= SUBMITの入力ボタンをクリックすることです。何らかの理由で応答しません。私はonClickイベントハンドラを呼び出すと一緒に.click()を使用しようとしましたが、何も動作していません。どんな助けもありがとうございます。 HTMLコードは以下の通りです:JavascriptとWebViewでHTML入力ボタンをクリックできません(おそらくiframeの問題、わからない)

<div class="fix_button"> 
    <table width="100%" border="0" cellpadding="0" cellspacing="2"> 
    <tbody> 
     <tr> 
     <td nowrap="" colspan="2" align="center"> 
      <input class="cancel_bt" type="RESET" name="Cancel" value="Cancel" onclick="doCancel();" languagecode="51"> 
      &nbsp; 
      <input class="apply_bt" type="SUBMIT" name="Apply" value="Apply" onclick="return checkData();" languagecode="50"> 
     </td> 
     </tr> 
    </tbody> 
    </table> 
</div> 

入力ボタンがclass="apply_bt"を有するものです。このdivはiframe内にあり、このフレーム内の別のiframe内にあるデータを送信します。構造は次のようにソートのです:

// Gets the outer iframe 
iframe1 = document.getElementById("formframe"); 
// Gets the html within the iframe 
var innerDoc=iframe1.contentDocument || iframe1.contentWindow.document; 
// Gets iframe within the iframe 
innerframe = innerDoc.getElementById("2g_setting"); 
// Gets the html within the inner iframe 
var innerDoc2=innerframe.contentDocument || innerframe.contentWindow.document; 

// I Enter some data in the inner iframe (not shown), this portion 
// of the code works perfectly. I can see fields being entered and 
// buttons being clicked. 
// Then I click the apply button... 
innerDoc.getElementsByName("Apply")[0].click() 
// And nothing happens 

私は、このような、要素を取得し、[適用]ボタンからデータを読み取ることができています:

<iframe> 
    // Contains the Apply Button 
    <div class="fix_button"></div> 

    // Contains the iframe where the info to be submitted is 
    <div id="main" class="main_top_button"> 
     // Contains data to be submitted 
     <iframe src="WLG_2g_wireless2.htm&amp;todo=wifi_primary_init" id="2g_setting" name="wl2gsetting" onload="SetCwinHeight(this.id)" marginheight="0" marginwidth="0" scrolling="no" width="100%" frameborder="0" height="288px"> 
     </iframe> 
    </div> 
</iframe> 

これは私がボタンをクリックして使用していたコードですボタンとして.click()アクションを実行することはできません。また、私は何の誤りもありません。私はなぜそれが動作しているのか分かりません。助けてください!

答えて

1

clickメソッドは<a>タグでのみ信頼性が高くなります。代わりに合成イベントを発生させる必要があります。これにより、html属性、element.onclick、addEventListener、または使用している可能性のあるライブラリとして設定されているかどうかに関係なく、すべてのイベントハンドラが呼び出されることが保証されます。

上記の質問からのコードを使用してHow can I trigger a JavaScript event click

を参照してください、次の操作を行います。

var button = innerDoc.getElementsByName("Apply")[0] ; 

fireEvent(button, 'click') 
+0

クイックアンサーに感謝します。あなたのコードを試しましたが、残念ながらそのコードは動作しませんでした。ボタンはまだクリックされません。まだエラーは表示されません。内部フレームにデータを入力する前のコードはうまく動作しますが、イベントはまだ発生しません。どんな考え?他の解決策はありますか? – metacore

0

問題は、スクリプトが別のiFrameの内部にあるということです、そしてあなたがそのiFrameの内部にない場合は、それをつかむことができません。したがって、onclickイベントは失敗します。外側のiFrameから関数を実行するには、まず内部のIframeから関数を取得する必要があります。

関連する問題