簡體   English   中英

點擊時未觸發onclick事件

[英]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,否則將無法正常工作

重命名功能。 在許多瀏覽器中,它們all具有預定義的含義,因此與固有事件屬性一起使用時,使其成為保留關鍵字(如果不是,實際上是有效的)。

非工作版本給出“ NS_ERROR_ILLEGAL_VALUE:非法值”。

工作版本all重命名為notall

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM