1

我不知道如何解决这个问题。我创建了一个 java 方法,它使用本地语言创建了一个包含所有信息的字符串:

public String getAllCountries(){
StringBuilder sb = new StringBuilder();
sb.append("[ ");
ResourceBundle bundlePays = ResourceBundle.getBundle(BUNDLE_PAYS, localeBean.getLocale());
for(int i = 0; i< bundlePays.keySet().size(); i++ ){
String key = (String) bundlePays.keySet().toArray()[i];
String nomPays = traduirePays(key);
ResourceBundle bundlePrefix = ResourceBundle.getBundle(BUNDLE_PREFIX);

        if ((AieStringUtils.getSafeBundleString(bundlePrefix, key)) != key){

            String prefix = bundlePrefix.getString(key);
            sb.append("[");
            sb.append("\"" + nomPays + "\"" + "," + "\""+ key +"\"" + "," + "\""+ prefix + "\"");
            if (i!=bundlePays.keySet().size()-1){
                sb.append("], ");
            }else{
                sb.append("] ");
            }
        }
    }
    sb.append(" ]");
    String allCountries = sb.toString();
    System.out.println(allCountries);
    return allCountries;

}

我把结果放在我的 xhtml 中,如下所示:

<inputTextArea type="hidden" name="allCountries" id="allCountries" 
                        value="#{pageInfoPerso.allCountries}"/>

它给了我这种结果:

[ ["Vanuatu","VU","678"], ["Vietnam","VN","84"], ["Equateur","EC","593"], ["Iles Vierges Americaines","VI","1"], ["Algerie","DZ","213"], ["Iles Vierges Britanniques","VG","1"], ["Venezuela","VE","58"] ]
etc

然后,我想在 intlTelInput.js 中使用它所以,我决定将 var 放在 js 中并以这种方式从 xhtml 插入:

<h:outputScript>
        jQuery(document).ready(function() {                     
                     $("#phoneNumber").intlTelInput({
                     var allCountries={allCountries : document.getElementById("allCountries").innerHTML};
                        utilsScript: "/js/utils.js"
                    });
                     $("#phoneNumber2").intlTelInput({
                        utilsScript: "/js/utils.js"
                    });
                });

    </h:outputScript>

它不起作用,我收到此错误:“SyntaxError:缺失:属性 id 之后”

在这一点上,我可能不得不告诉你,我之前从未做过任何正面工作,也从未接触过 Javascript:/

我也直接试过

 the js file to put my var like this at the top of it :

//var allCountries = document.getElementById("allCountries").innerHTML;
//
//for (var i = 0; i < allCountries.length; i++) {
//    var c = allCountries[i];
//    allCountries[i] = {
//        name: c[0],
//        iso2: c[1],
//        dialCode: c[2],
//        priority: c[3] || 0,
//        areaCodes: c[4] || null
//    };
//}

它也没有工作,我收到了这条消息:

“this.countries[0] 是未定义的国际电话输入”

有人可以向我解释一下如何将我的字符串从 java 传递到 html 到 Js 吗?非常感谢你。我找了两天了,我快疯了。

4

0 回答 0