如何在 JSP 页面中的选项标签上使用 onClick() 或 onSelect()?

IT技术 javascript
2021-01-30 12:56:35

如何使用onClick()onSelect()option标签?下面是我尝试实现它的代码,但它没有按预期工作。

注意: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>   

如何修改它以检测选择了一个选项?

6个回答

标签既不支持onSelect()也不onClick()支持事件<option>前者是指选择文本(即通过在文本字段上单击+拖动),因此只能与<text><textarea>标签一起使用onClick()事件可以与<select>标签一起使用- 但是,您可能正在寻找最好使用该onChange()事件的功能,而不是onClick().

此外,从<c:...>标签的外观来看,您还试图在纯 HTML 文档中使用JSP语法。那只是……不正确。

回应你对这个答案的评论 - 我几乎无法理解。但是,听起来您想要做的是<option>在用户选择一个标签时获取用户刚刚选择标签的值在这种情况下,您希望拥有以下内容:

<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>
@Stephen .. 使用上面的代码,我正在创建显示客户姓名的下拉菜单。如果我在下拉菜单中选择任何一个值,该值应该传递给 java 脚本.. 请帮我怎么做..
2021-03-12 12:56:35
@Stephen .. 我的要求是我有包含 n 个客户名称的下拉列表菜单(显示在 jsp 页面中)。如果我单击该列表中的任何名称,则所选名称应发送到 javascript 函数。请为此提供解决方案..
2021-03-12 12:56:35
啊,所以您正在使用 Java Server Pages (JSP)。抱歉,这超出了我的经验范围。我只使用过一次 JSP,那是为了生成一个 XML 文件,而不是正确的 HTML。我建议你谷歌一些关于 JSP 的教程。对不起。
2021-03-29 12:56:35
@Stephen .. 我有超过 1000 个客户名称..在这种情况下,我如何创建选项#1、选项#2。而且数据是动态的
2021-04-05 12:56:35
在这种情况下,您可能希望在服务器端填充它。您在服务器端使用 PHP 还是其他语言?
2021-04-05 12:56:35

更简化:您可以直接传递value属性!

<html>
 <head>
  <script type="text/javascript">

   function changeFunc(i) {
     alert(i);
   }

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

警报将返回12

您在上面给出的答案有效,但令人困惑,因为您使用了两个名称两次,并且有一行不必要的代码。你正在做一个不必要的过程。

在调试代码时,用笔和纸画小盒子来表示内存空间(即存储的变量),然后画箭头来指示变量何时进入小盒子以及何时出现,这是一个好主意覆盖或复制等。

如果您使用下面的代码执行此操作,您将看到

var selectBox = document.getElementById("selectBox");

被放在一个盒子里并留在那里,之后你不会对它做任何事情。

var selectBox = document.getElementById("selectBox");

很难调试,并且当您为选项列表中选择SelectBox的选择ID时,令人困惑。---- 您要操作/查询/等哪个selectBox 是将消失的本地var selectBox 还是您分配给select 标签的selectBox id

您的代码在您添加或修改它之前一直有效,然后您可以轻松地失去跟踪并混淆

<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>

一种更精简的工作方式也是:

<html>
<head>
<script type="text/javascript">

function changeFunc() {

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>

使用与您正在处理的程序和任务相匹配的描述性名称是个好主意,我目前正在编写一个类似的程序来使用您的代码接受和处理邮政编码,并使用描述性名称对其进行修改 目的是使计算机语言接近自然语言尽可能。

<script type="text/javascript">

function Mapit(){

var actualPostcode=getPostcodes.options[getPostcodes.selectedIndex].value;

alert(actualPostcode);

// alert is for debugging only next we go on to process and do something
// in this developing program it will placing markers on a map

}

</script>

<select id="getPostcodes" onchange="Mapit();">

<option>London North Inner</option>

<option>N1</option>

<option>London North Outer</option>

<option>N2</option>
<option>N3</option>
<option>N4</option>

// a lot more options follow 
// with text in options to divide into areas and nothing will happen 
// if visitor clicks on the text function Mapit() will ignore
// all clicks on the divider text inserted into option boxes

</select>
 <div class="form-group">
         <script type="text/javascript"> 
              function activa(){                                                            
                 if(v==0)
                     document.formulario.vr_negativo.disabled = true; 
                 else if(v==1)
                      document.formulario.vr_negativo.disabled = true; 
                 else if(v==2)
                      document.formulario.vr_negativo.disabled = true; 
                 else if(v==3)
                      document.formulario.vr_negativo.disabled = true; 
                 else if(v==4)
                      document.formulario.vr_negativo.disabled = true; 
                 else if(v==5)
                      document.formulario.vr_negativo.disabled = true; 
                 else if(v==6)
                      document.formulario.vr_negativo.disabled = false;}                                                   
        </script> 
        <label>&iquest;Qu&eacute; tipo de veh&iacute;culo est&aacute; buscando?</label>
        <form name="formulario" id="formulario">
          <select name="lista" id="lista" onclick="activa(this.value)"> 
              <option value="0">Vehiculo para la familia</option> 
              <option value="1">Vehiculo para el trabajo</option> 
              <option value="2">Camioneta Familiar</option> 
              <option value="3">Camioneta de Carga</option> 
              <option value="4">Vehiculo servicio Publico</option>
              <option value="5">Vehiculo servicio Privado</option>
              <option value="6">Otro</option>
          </select> 
          <br />
          <input type="text" id="form vr_negativo" class="form-control input-xlarge" name="vr_negativo"/>
        </form>
  </div>

在这个例子中 de select 标签被命名为:aula_clase_cb

<select class="form-control" id="aula_clase_cb" >
</select>

document.getElementById("aula_clase_cb").onchange = function(e){
    id = document.getElementById('aula_clase_cb').value;
    alert("id: "+id);
};