Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

javascript - 如何在JSP页面的选项标签上使用onClick()或onSelect()?(How to use onClick() or onSelect() on option tag in a JSP page?)

How to use onClick() or onSelect() with option tag?(如何使用带有option标签的onClick()onSelect() ?)

Below is my code in which I tried to implement that, but it is not working as expected.(以下是我尝试实现该代码的代码,但未按预期工作。)

Note: where listCustomer domain object list getting in JSP page.(注意:其中listCustomer域对象列表在JSP页面中获得。)

<td align="right"> 
  <select name="singleSelect" "> 
     <c:forEach var="Customer" items="${listCustomer}" >
     <option value="" onClick="javascript:onSelect(this);> <c:out value="${Customer}" /></option>
                </c:forEach>
          </select>         
        </td>   

How do I modify it to detect that an option is selected?(如何修改它以检测到选择了一个选项?)

  ask by Manu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Neither the onSelect() nor onClick() events are supported by the <option> tag.(<option>标记不支持onSelect()onClick()事件。)

The former refers to selecting text (ie by clicking + dragging across a text field) so can only be used with the <text> and <textarea> tags.(前者是指选择文本(即通过单击+在文本字段上拖动),因此只能与<text><textarea>标记一起使用。) The onClick() event can be used with <select> tags - however, you probably are looking for functionality where it would be best to use the onChange() event, not onClick() .(onClick()事件可以与<select>标记一起使用-但是,您可能正在寻找最好使用onChange()事件而不是onClick() 。)

Furthermore, by the look of your <c:...> tags, you are also trying to use JSP syntax in a plain HTML document.(此外,通过<c:...>标记的外观,您还试图在纯HTML文档中使用JSP语法。)

That's just... incorrect.(那只是...不对。)

In response to your comment to this answer - I can barely understand it.(回应您对这个答案的评论-我几乎无法理解。)

However, it sounds like what you want to do is get the value of the <option> tag that the user has just selected whenever they select one.(但是,听起来您想做的就是获取用户选择时刚刚选择的<option>标记的值。) In that case, you want to have something like:(在这种情况下,您希望拥有以下内容:)
<html>
 <head>
  <script type="text/javascript">

   function changeFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    alert(selectedValue);
   }

  </script>
 </head>
 <body>
  <select id="selectBox" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
 </body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...