2011-09-12 14 views
1

Grails site exampleコードを使用してajax selectを実装しました。ユーザーが国を選択し、都市フィールドが更新されます。 PrototypeではなくjQueryを使用しています。AjaxはGrailsでリフレッシュしない選択を選択します

新しい国を選択すると、都市フィールドは変更されませんが、ユーザーが都市選択をクリックすると、新しい都市のみが表示され、ユーザーはその都市の1つを選択できます。

都市選択リストは更新されていますが、現在表示されている値は自動的に更新されません。

これは図である。ここでは

 <div data-role="fieldcontain"> 
      <label for="country">Country</label> 
      <g:select 
        optionKey="id" optionValue="name" name="country" id="country" 
        from="${com.TourneyCard.Country.list(sort:'name')}" value="${homeCountry.id}" 
        onchange="${remoteFunction(
         action:'ajaxGetCityJSON', 
         params:'\'id=\' + escape(this.value)', 
         onSuccess:'updateCity(data);')}"> 
      </g:select> 
     </div> 

     <div data-role="fieldcontain"> 
      <label class="ui-select" for="city">City</label> 
      <g:select name="city" id="tee" data-native-menu="true" 
         optionKey="id" optionValue="name" from="${homeCities}"> 
      </g:select> 
     </div> 

はjavascriptのです:

<g:javascript> 
    function updateCity(data) { 
     if (data) { 
      var rselect = document.getElementById('city') 

      var l = rselect.length 

      while (l > 0) { 
       l-- 
       rselect.remove(l) 
      } 

      for (var i = 0; i < data.length; i++) { 
       var tee = data[i] 
       var opt = document.createElement('option'); 
       opt.text = city.name 
       opt.value = city.id 
       try { 
        rselect.add(opt, null) // standards compliant; doesn't work in IE 
       } 
       catch(ex) { 
        rselect.add(opt) // IE only 
       } 
      } 
     } 
    } 
    window.onload = function() { 
     var zselect = document.getElementById('city') 
     var zopt = zselect.options[zselect.selectedIndex] 
     ${remoteFunction(
      action: "ajaxGetCityJSON", 
      params: "'id=' + zopt.value", 
      onSuccess: "updateCity(data)")} 
     } 
</g:javascript> 
+0

大した質問ですが、スタイル的には、私は小文字の「L」を*任意の*プログラミング言語の変数として使用しないでください。多くのフォントでは、lと1の差を判別することは非常に困難です。 1 = l、even |そして、lと1 '1 = 'L' – avgvstvs

+0

あなたが試すことができ、このaproach [http://stackoverflow.com/questions/17450294/grails-select-remotefunction-not-working][1] [ 1]:http://stackoverflow.com/questions/17450294/grails-select-remotefunction-not-working –

答えて

0

あなたが都市選択リストに新しいオプションに追加した後に次の行を追加します。

rselect.selectedIndex = 0

このリスト内の選択オプションをリセットし、ユーザーの介入なしに再表示リストにブラウザを強制する必要があります。

+0

selectedIndex値を設定すると、選択されている値は変更されますが、まだ表示が更新されません。たとえば、selectedIndexが1に設定されていて選択フィールドに触れた場合、アイテム1はゼロの代わりに選択されていますが、フィールドに触れることなく古い値の1つを表示します。 – SeattleStephens

関連する問題