假设我有JSF页面如何JSF变量传递给jQuery的
<h:body>
<h:form id="formID" prependId="false">
<h:outputText id="randomID" value="#{randomNumber.random}"/>
<h:commandButton id="buttonID"
value="Random"
onclick="myArrayList(countries)" /> //just example
</h:form>
</h:body>
@ViewScoped
public class RandomNumber {
private int totalCountries;
private String data = "Afghanistan, Albania, Zimbabwe";
private List<String> countries;
public RandomNumber() {
countries = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(data, ",");
while(st.hasMoreTokens()) {
countries.add(st.nextToken().trim());
}
totalCountries = countries.size();
} //end of constructor
} //end of class RandomNumber
js文件
function myArrayList(countries) {
.....
}
见当按钮用户点击的话,我想打电话给我为之传递jQuery函数我数组列表。是否有可能将您当前的JSF变量的值传递给JavaScript或jQuery?
感谢
编辑 ** _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ - *
<h:body>
<h:form id="formID" prependId="false">
<h:outputText id="countries" value="#{countries.countries}" style="display:none"/>
<h:inputHidden id="hiddenCountries" value="#{countries.countries}" />
<h:commandButton id="buttonID"
value="Random"
onclick="myArrayList()"/>
</h:form>
</h:body>
@ViewScoped
public class Countries {
private int totalCountries;
private String data = "Afghanistan, Albania, Zimbabwe";
private List<String> countries;
/** Creates a new instance of Countries */
public Countries() {
countries = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(data, ",");
while(st.hasMoreTokens()) {
countries.add(st.nextToken().trim());
}
totalCountries = countries.size();
} //end of constructor
public List<String> getCountries() {
return countries;
}
public void setCountries(List<String> countries) {
this.countries = countries;
}
} //end of class Countries
function myArrayList() {
alert(jQuery('#countries').html());
alert(jQuery('#hiddenCountries').val()) //when using h:inputHidden
} //end of function myArrayList
但这会给你的outputText.Not ArrayList(国家)的值?我想我不能这样做,因为我的ArrayList是服务器端代码,jQuery是客户端。如何通过调用函数将我的服务器代码传递给jquery ....我不知道我是否正确? – Basit 2012-04-02 13:59:27
对不起,延迟回复。谢谢:)是的,你是对的,我正在获取ArrayList中的所有值。我编辑我的问题。但是,我点击警告框后,我也收到错误信息。 **'转换器'的转换错误设置值'[阿富汗,阿尔巴尼亚,津巴布韦]'。 **。为什么我得到这个?还有一个问题。超出范围,但与之相关...好吧,我将我的Java变量值传递给了jQuery。如果我想让我的ArrayList在JQuery中变为空,该怎么办?就像我想要的那样,我在jQuery中使用了类似的东西。 '国家= null',这件事反映在我的Java代码中。可能吗 ? – Basit 2012-04-03 05:01:09
编辑我的问题 – Daniel 2012-04-03 05:13:06