search
HomeWeb Front-endHTML Tutorial求助HTML高手radio如何响应失去选中事件_html/css_WEB-ITnose


呵呵~!

我做的东西比较恶心

现在遇到了个问题就是input的radio控件响应事件问题

是这样:一个radio不光要响应被选中的事件,还要监听自己被撤销选中的事件

比如我有个选择题,A,B,C三个选项

用户在页面先选了A,根据某些业务逻辑,选了A以后是要进行一些相关操作的

但是这时候用户想了一下,觉得选A不好,改选B了

那么刚才根据用户选了A这一结果执行的操作,就要实现逆操作

我想设置radio的 onpropertychange事件来实现这个操作,但是发现把JavaScript方法注册到事件上以后

用户点中radio的时候,浏览器调用了2次该方法

而且似乎是在radio的checked属性被设置为true之前调用一次,设置为true之后调用一次

而当选了同组其他radio的时候,这个方法只执行了一次

很郁闷

所以来问问

有没有方法监听radio控件被选中和被撤销的事件啊?

谢谢大家了


回复讨论(解决方案)

换个思路。如果改选B,那么会触发事件。这样同样可以监听A撤消啊

试试 onclick 

用一个全局变量记住当前选项

<html> <script language="javascript" defer="defer"> var cur;function check(){	var val;    var rds = document.forms[0].rd;    for(var i=0;i<rds.length;i++){        if(rds[i].checked){            val = i;            break;	        }	    }    if(val != cur){cur = val; alert("changed")}} window.onload=function(){    var rds = document.forms[0].rd;    for(var i=0;i<rds.length;i++){        if(rds[i].checked){            cur = i;            break;	        }	    }}</script> <body><form><input type="radio" name="rd" value="0" checked="checked" onclick="check()"> 0<br><input type="radio" name="rd" value="1" onclick="check()"> 1<br><input type="radio" name="rd" value="2" onclick="check()"> 2<br></form></body> </html>

<div id=radios> a <input type=radio name=a value=a > b <input type=radio name=a value=b> c <input type=radio name=a value=c></div><script language="javascript"><!--var obj=document.getElementById("radios").getElementsByTagName("input")for (var i=0;i<obj.length;i++){(function(k){obj[i].onclick=function(){cha(k)}})(i)}var lastChecked=nullfunction cha(k){if (lastChecked==k)returnif (lastChecked!=null){	//处理obj[lastChecked]	alert("上次选中:"+lastChecked)}lastChecked=k//处理obj[lastChecked]alert("本次选中:"+lastChecked)}//--></script> 

使用临时变量是不行的

因为页面上的选择题的数量,以及选择题和填空题

以及题目之间的跳转关系

比如:单选某一选项要跳转到哪个题
      选择题之间选项的组合,选择题与填空题之间的组合

等等都是用户在设计试卷的时候指定的

不是写死在页面上的

变量可以动态创建,或用数组,不需要写死。

路过 学习

用一个全局变量记住当前选项 

HTML code
 
<script> <br /> var cur; <br /> <br /> function check(){ <br /> var val; <br /> var rds = document.forms[0].rd; <br /> for(var i=0;i<rds.length;i++){ <br /> if(rds[i].checked){ <br /> val = i; <br /> break; <br /> } <br /> } <br /> if(val != cur){cur = val; alert("changed")} <br /> } <br /> window.onload=function(){ <br /> v… <br /> <br /> 正解! </script>

打酱油

失去选中,也即失去焦点,那么失去焦点的事件可以在对象中这样响应
...onblur="javascript:workFunName(参数);"...

失去选中,也即失去焦点,那么失去焦点的事件可以在对象中这样响应 
...onblur="javascript:workFunName(参数);"...

绝错~

你选A选项以后,再去填一个空

然后回来看这个选择题,改成了选B,此时,A失去选中,但不会触发失去焦点事件

我想的和2楼的思路一样

唉~

做一个调查问卷实在是太难了~

做了个测试

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title></head><body><form method="post" action="">	<input type="radio" name="ask1" id="a1" value="v1" />a1	<input type="radio" name="ask1" id="a2" value="v2" />a2	<input type="radio" name="ask1" id="a3" value="v3" />a3</form></body><script type="text/javascript">//<![CDATA[function a1(){	alert('a1 ....   do something');}function a2(){	alert('a2 ....  do something');}function a3(){	alert('a3 ....   do something ');}function setradio(oname, v, fn){	var rdos = document.getElementsByName(oname);	for ( var i=0,L=rdos.length; i<L; i++ ) {		if ( rdos[i].value==v ) {			rdos[i].onclick = function(){				fn();			};		}	}}setradio('ask1', 'v1', a1);setradio('ask1', 'v2', a2);setradio('ask1', 'v3', a3);//]]></script></html>

恩!

谢谢大家

帖子结了 

目前的思路是和xiaojing7的想法一样的

感谢大家的回复,以后有问题,我还会来骚扰大家的

谢谢

欢迎骚扰

真恶心

asdfadsasdf

function getRadio(obj) {
alert(obj.value);
var logon = document.getElementById("logon");
var reg = document.getElementById("reg");

}


1

2

没事别动不东就自动执行 onload什么的

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment