2014-06-14 113 views
0

我正面临一个微小的问题..单选按钮的onclick动作没有执行。我试着用onchange事件为好,但没有用的..onclick事件没有点击发射

<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文件链接,以及)

+0

尝试添加'onclick'标签。 'Onclick'应该在'radiobutton'上工作。处理事件后可能会写入函数。 – CnapoB

+0

重复http://stackoverflow.com/questions/8838648/onchange-event-handler-for-radio-button-input-type-radio-doesnt-work-as-one – chandu

+1

为什么要用相同的名称编号? –

回答

0

编辑:这确实与allDemo

首先,使用两个不同的输入具有相同ID的工作。你不能那样做。

其次,尽量从JS的分隔条件HTML: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。

+0

为什么这会降低投票率? – rottenoats

-1
$(document).ready(function() 
     { 

      $('body').click(function() 
      { 
       alert('hi'); 
      }); 
     }); 

此处将“body”标签更改为您希望功能发生的对象的ID。 请确保您使用的是jquery,否则这将无法正常工作

+0

不工作...控制不会在那里...是它的JavaScript或jQuery的:( –

+0

再试一次。我没有克服文件就绪功能在第一。@SamuelMathews –

-1

重命名该函数。 all在许多浏览器中具有预定义的含义,使其在内部事件属性中使用时有效(如果不是实际上)保留关键字。

Non-working version给出“NS_ERROR_ILLEGAL_VALUE:Illegal value”。

Working version已将all更名为notall