2012-04-02 84 views
2

假设我有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 

回答

1

你可以做

<h:inputHidden id="someID" value="#{randomNumber.data}/> 

(不要忘记添加getter/setter到你的bean中的数据)

改变你的h:commandButton

onclick="myArrayList()" // or onclick="myArrayList(#{SomeELExpressioGoesHere})" 

变化的onclick您js函数到

function myArrayList(inputParam) { 
    alert(jQuery('#someID').val()); 
    var yourJSString = jQuery('#someID').val(); 
    var myArray = yourJSString.split(','); //when string is Afghanistan,Albania,Zimbabwe (no spaces) 
    alert(myArray[inputParam]); 
} 

为了您的js的字符串转移到阵列只使用

var yourJSString = jQuery('#someID').val() 
var myArray = yourJSString.split(','); //when string is Afghanistan,Albania,Zimbabwe (no spaces) 

你可能会需要将someID更改为其他id选择器,如果你不习惯设置prependId =表单中的假......或者干脆使用broaded jQuery选择像

alert(('input[id$="someID"]').val()); 

编辑

看我的例子,你应该用你的字符串数据的变量,而不是国家工作,因为你应该只将数据作为字符串传递,而不是数组......在将数据变量传递给js后,您可以使用简单的slpit js函数对其进行操作,使其返回js中的数组...您将获得转换错误设置值'[阿富汗,阿尔巴尼亚,津巴布韦]'因为你试图将字符串设置为数组...也许国家的吸气转换隐含的Java数组转换为字符串,当你试图提交回(字符串转换成数组),你得到的异常

为了从JS设置回至豆:

jQuery('#someID').val("Some new String goes here"); 

然后当你将提交表格..价值将被设置回服务器

+0

但这会给你的outputText.Not ArrayList(国家)的值?我想我不能这样做,因为我的ArrayList是服务器端代码,jQuery是客户端。如何通过调用函数将我的服务器代码传递给jquery ....我不知道我是否正确? – Basit 2012-04-02 13:59:27

+0

对不起,延迟回复。谢谢:)是的,你是对的,我正在获取ArrayList中的所有值。我编辑我的问题。但是,我点击警告框后,我也收到错误信息。 **'转换器'的转换错误设置值'[阿富汗,阿尔巴尼亚,津巴布韦]'。 **。为什么我得到这个?还有一个问题。超出范围,但与之相关...好吧,我将我的Java变量值传递给了jQuery。如果我想让我的ArrayList在JQuery中变为空,该怎么办?就像我想要的那样,我在jQuery中使用了类似的东西。 '国家= null',这件事反映在我的Java代码中。可能吗 ? – Basit 2012-04-03 05:01:09

+0

编辑我的问题 – Daniel 2012-04-03 05:13:06