简体   繁体   English

onclick事件未在HTML上运行选择下拉列表

[英]onclick Event Not Running on HTML Select Drop Down

I currently have a HTML select drop down with six options all I'm trying to achieve is when the option "Other" is selected a javascript alert appears, but for some reason I cannot get this to work. 我目前有一个HTML选择下拉列表,其中有六个选项我想要实现的是当选择“其他”选项时出现javascript警告,但由于某种原因我无法使其工作。

I've tried moving the onclick to the select tag rather than the option tag, and moving the alert outside the parameters which allows it to appear as soon as the drop down is clicked which worked but isn't what I'm trying to achieve. 我已经尝试将onclick移动到选择标签而不是选项标签,并将警报移到参数之外,这样一旦点击下拉菜单就可以显示它,但这不是我想要实现的。

 function otherPayment() { var paymentType = document.getElementById("paymentType").value; if (paymentType == "Other") { alert("test"); } } 
 <div class="form-group"> <label for="paymentType">Payment Type:</label> <select class="form-control" id="paymentType" name="paymentType" required> <option value="">-</option> <option value="Payment (Initial)">Payment (Initial)</option> <option value="Payment (Full)">Payment (Full)</option> <option value="Payment (Balance)">Payment (Balance)</option> <option value="Delivery Fee">Delivery</option> <option onclick="otherPayment()" value="Other">Other</option> </select> </div> 

<option> elements don't behave like normal HTML elements, so onclick doesn't do anything. <option>元素的行为与普通的HTML元素不同,因此onclick不会执行任何操作。

The easiest way to do this is to use the onchange event of the select, and then inspect the newly selected element, like this: 最简单的方法是使用select的onchange事件,然后检查新选择的元素,如下所示:

 function handlePaymentChange(event) { var paymentType = event.target.value; if (paymentType == "Other") { alert("test"); } } 
 <div class="form-group"> <label for="paymentType">Payment Type:</label> <select class="form-control" id="paymentType" name="paymentType" required onchange="handlePaymentChange(event)"> <option value="">-</option> <option value="Payment (Initial)">Payment (Initial)</option> <option value="Payment (Full)">Payment (Full)</option> <option value="Payment (Balance)">Payment (Balance)</option> <option value="Delivery Fee">Delivery</option> <option value="Other">Other</option> </select> </div> 

You have to use onchange on the select tag not on the option tag. 你必须使用onchangeselect标签上没有option标签。

 function otherPayment() { var paymentType = document.getElementById("paymentType").value; if (paymentType == "Other") { alert("test"); } } 
 <div class="form-group"> <label for="paymentType">Payment Type:</label> <select class="form-control" id="paymentType" name="paymentType" required onchange="otherPayment()"> <option value="">-</option> <option value="Payment (Initial)">Payment (Initial)</option> <option value="Payment (Full)">Payment (Full)</option> <option value="Payment (Balance)">Payment (Balance)</option> <option value="Delivery Fee">Delivery</option> <option value="Other">Other</option> </select> </div> 

The easiest way to do this would be to use onselect : 最简单的方法是使用onselect

<div class="form-group">
  <label for="paymentType">Payment Type:</label>
  <select class="form-control" id="paymentType" name="paymentType" onselect="otherPayment()" required>
    <option value="">-</option>
    <option value="Payment (Initial)">Payment (Initial)</option>
    <option value="Payment (Full)">Payment (Full)</option>
    <option value="Payment (Balance)">Payment (Balance)</option>
    <option value="Delivery Fee">Delivery</option>
    <option value="Other">Other</option>
  </select>
</div>

Since there is already an if/else , it should work well. 由于已经存在if/else ,因此它应该运行良好。

我建议绑定下拉列表的onchange事件而不是选项的click事件。

 <select class="form-control" id="paymentType" onchange="otherPayment()" name="paymentType" required>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM