[英]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.