如何在Onclick功能中传递多个Id [英] How Can I Pass Multiple Id In A Onclick Function

查看:204
本文介绍了如何在Onclick功能中传递多个Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在fom验证系统中我想将所有表单验证js文件合并到一个公共js文件中,以便相应的html文件文本文件,密码字段id,文本区域id在onclick函数内发送

in a fom validation system i want to combine all form validation js file into a common js file so corresponding html file text file, password field id,text area id's sent inside onclick function

推荐答案

为要验证的每个字段或元素在全局或类/对象中创建变量,例如



for each of the fields or elements you want to validate make variables globally or in a class/object e.g

var passfield = document.getElementById('id');





然后检查值/内容。



then check the values/content.


这个事件的处理程序接受它接受的方法参数, MouseEvent 对象,没有别的:

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/ XUL /属性/ onclick [ ^ ],

https://developer.mozilla.org/en-US/docs/Web/Events/click [< a href =https://developer.mozilla.org/en-US/docs/Web/Events/clicktarget =_ blanktitle =New Window> ^ ],

https://developer.mozilla.org/en-US/docs/Web/API / MouseEvent [ ^ ]。



因此,您可以通过外部环境传递所需的身份信息,例如

This event's handler accepts the method arguments it accepts, MouseEvent object, nothing else:
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/onclick[^],
https://developer.mozilla.org/en-US/docs/Web/Events/click[^],
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent[^].

So, you can pass required id information though outside context, such as
var idSet = // some object representing id set,
            // such as array

//...

myElement.onclick = function(eventInfo) {
   var x = eventInfo.screenX;
   var y = eventInfo.screenY;
   doSomething(x, y, idSet);
}





或者,您可以在处理程序中按ID计算元素,但它将在每个事件上完成,并且没有多大意义,因为最好重用元素引用或 id 属性的值:





Alternatively, you can calculate the elements by id inside the handler, but it will be done on each event, and make not much more sense, because it's better to reuse the element references or the values of id attributes:

myElement.onclick = function(eventInfo) {
   var x = eventInfo.screenX;
   var y = eventInfo.screenY;
   var myTextArea = document.getElementById("text");
   var myInput = document.getElementById("input");
   doSomething(x, y, myTextArea, myInput); // whatever
}





-SA


这篇关于如何在Onclick功能中传递多个Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆