最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Call specific JavaScript function when specific item of h:selectOneMenu is selected - Stack Overflow

programmeradmin1浏览0评论

I have with ajax

<h:selectOneMenu>
<f:selectItem itemLabel="please click"/>
<f:selectItem itemLabel="1"/>
<f:selectItem itemLabel="2"/>
<f:ajax onevent="click()" />
</h:selectOneMenu>

I have with onChange

<h:selectOneMenu onChange="click()>
<f:selectItem itemLabel="please click"/>
<f:selectItem itemLabel="1"/>
<f:selectItem itemLabel="2"/>    
</h:selectOneMenu>

if I try the same attributes to selectItem, it doesnt work. I think they dont exist.

I used ajax, onChange but it seem to wrok for selectonemenu but not in particular for selectItem.

I have with ajax

<h:selectOneMenu>
<f:selectItem itemLabel="please click"/>
<f:selectItem itemLabel="1"/>
<f:selectItem itemLabel="2"/>
<f:ajax onevent="click()" />
</h:selectOneMenu>

I have with onChange

<h:selectOneMenu onChange="click()>
<f:selectItem itemLabel="please click"/>
<f:selectItem itemLabel="1"/>
<f:selectItem itemLabel="2"/>    
</h:selectOneMenu>

if I try the same attributes to selectItem, it doesnt work. I think they dont exist.

I used ajax, onChange but it seem to wrok for selectonemenu but not in particular for selectItem.

Share Improve this question edited May 6, 2015 at 18:02 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked May 6, 2015 at 17:06 ashleshaashlesha 1091 gold badge2 silver badges12 bronze badges 3
  • Your question is ambigious. You asked to fire an "action" which we understand to be a JSF managed bean action (or listener) method, but your code snippets instead attempt to fire a JavaScript function. Also, the presence of [javascript] tag on the question and explicit usage of on* attribute suggests that you want to do it client side rather than server side. What exactly do you want? Better yet, what problem/requirement exactly are you ultimately trying to solve and you thought that this all would possibly be the right solution? – BalusC Commented May 6, 2015 at 17:19
  • I want to have an action on the client side by calling a js method. The code snippet works for the entire selectOneMenu ( select any items in the menu and js method is called). Instead i want to have different js methods to be called for different select items) – ashlesha Commented May 6, 2015 at 17:54
  • Why not just let the function check which item is currently being selected and then delegate further to the right task/function? I fixed the question title and posted an answer. – BalusC Commented May 6, 2015 at 17:58
Add a ment  | 

1 Answer 1

Reset to default 6

Just pass the selected value to the JS function which in turn delegates further based on it.

<h:selectOneMenu onchange="foo(this.value)">
    ...
</h:selectOneMenu>
function foo(selectedValue) {
    switch (selectedValue) {
        case "1": bar(); break;
        case "2": baz(); break;
        // ...
    }
}
发布评论

评论列表(0)

  1. 暂无评论