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