![](/img/trans.png)
[英]How to avoid firing an onclick() event fired when clicking a link in AngularJS
[英]onclick event not fired on clicking
我面临一个奇怪的小问题。.单选按钮的onclick操作未执行。 我也尝试过onchange事件,但是没有用。
<label><input type="radio" name="search_type" id="search_type" value="name" onchange="by_name()">Enter the Name </label>
<input name="tag" type="text" id="search" maxlength="30">
<label><input type="radio" name="search_type" id="search_type" value="all" onchange="all()">All</label>
然后点击全部,
function all()
{
window.alert('hi');
}
能为您提供建议吗?(js文件也已链接)
编辑:它确实与all
演示一起工作
首先,您使用具有相同ID的两个不同输入。 你不能那样做。
其次,尝试将HTML与JS分开: DEMO
HTML:
<label>
<input type="radio" name="search_type" id="search_type" value="name">Enter the Name</label>
<input name="tag" type="text" id="search" maxlength="30">
<label>
<input type="radio" name="search_type" id="search_type2" value="all">All</label>
Javascript:
var input1 = document.getElementById('search_type');
var input2 = document.getElementById('search');
var input3 = document.getElementById('search_type2');
input1.onclick = function(){
alert('input1');
};
input2.onclick = function(){
alert('input2');
};
input3.onclick = function(){
alert('input3');
};
为什么要分开它们?
它是现代的,如果您有很多HTML,则将JavaScript保存在单独的文件中会更容易阅读。
$(document).ready(function()
{
$('body').click(function()
{
alert('hi');
});
});
在这里,将“ body”标签更改为要对其执行功能的对象的ID。 确保您使用的是Jquery,否则将无法正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.