2016-04-04 15 views
1

私のフォームには、他の2つのフィールドの値を取るボタンがあります。問題は、それらのフィールドの1つを入力した後にボタンをクリックすると、そのフィールドをクリックする前にそのフィールドの値が入力される前です(通常は空白)。 Javascriptを使用してそのフィールドをクリックしてシミュレートする方法はありますか?ボタンのクリック時(コードの残りの部分が実行される前)に別のフィールドにフォーカスを設定しようとしましたが、何らかの理由でフィールドの古い値が引き続き使用されています。Dynamics CRM:Javascriptでフィールドをクリックしてシミュレートする方法

function btnCalculate_onClick() { 
    Xrm.Page.ui.controls.get("zipCodeFieldName").setFocus(); 

    strStreet = Xrm.Page.getAttribute("streetFieldName").getValue(); 
    strCity = Xrm.Page.getAttribute("cityFieldName").getValue(); 
    strState = Xrm.Page.getAttribute("stateFieldName").getValue(); 

    $.ajax({ 
    url: 'https://maps.googleapis.com/maps/api/geocode/xml?address=' + strStreet + ',' + strCity + ',' + strState + '&sensor=false', 
    type: 'GET', 
    dataType: 'text', 
    timeout: 1000, 
    error: function() { 
     alert('Error loading XML document'); 
    }, 
    success: function(xml) { 
     // do something with xml 
    } 
    }); 
} 

問題は、私は最初のボタンをクリックする前に、市場の外にクリックしない限り、strCityがnullとして認識されていることである。

は、ここに私のコードの構造です。

+0

リンクかそこらのコードスニペットを投稿してください私たちは見るべきことがあります。 – Sparky256

+0

コードを含むように投稿を更新しました。 – Mike

+1

[blurイベント](https://developer.mozilla.org/en/US/docs/Web/Events/blur)を探しているようですね – Cypher

答えて

0

あなたはjQueryを使ってクリックイベントをトリガすることができます

$("#foo").trigger("click"); 

詳細here

0

を私はは、setFocusのものが働いているだろうと思っているだろう。これは私が使用するものです。この

function btnCalculate_onClick() { 
    $(document.body).click(); 

    setTimeout(doAddressStuff, 10); 
} 

function doAddressStuff() { 
    strStreet = Xrm.Page.getAttribute("streetFieldName").getValue(); 
    strCity = Xrm.Page.getAttribute("cityFieldName").getValue(); 
    strState = Xrm.Page.getAttribute("stateFieldName").getValue(); 

    $.ajax({ 
     url: 'https://maps.googleapis.com/maps/api/geocode/xml?address=' + strStreet + ',' + strCity + ',' + strState + '&sensor=false', 
     type: 'GET', 
     dataType: 'text', 
     timeout: 1000, 
     error: function() { 
     alert('Error loading XML document'); 
     }, 
     success: function(xml) { 
     // do something with xml 
     } 
    }); 

} 
1

てみ

function blurActiveControl() { 
    var elid = document.activeElement.id; 
    if (elid && elid != null && elid != '') document.activeElement.blur(); 
} 

サポートされていないが、安全かつ効果的な、あなたのケースでの使用には、次のようになります。

function btnCalculate_onClick() { 
    blurActiveControl(); 

    // rest of code 
} 
関連する問題