求助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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.
