[英]how can pass values from one function to an HTML callback?
var sno=10;
row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord('sno');'";
I want to pass sno
value to another function.我想将
sno
值传递给另一个函数。
function editRecord(mySno){}
Try to not mix codes.尽量不要混合代码。
var newRow = row.insertCell(6),
input = document.createElement('input');
This method is not just faster than the string innerHTML, it will allow you to handle the input as a JavaScript object.这种方法不仅比字符串innerHTML 更快,它还允许您将输入作为JavaScript 对象处理。
input.type = 'button';
input.value = 'edit';
input.onclick = editRecord;
You have created a new row, but if you were not creating a new one, you can clear the code inside the row:您创建了一个新行,但如果您没有创建一个新行,您可以清除该行内的代码:
newRow = (function clearHtmlElement (el){
var newEl = el.cloneNode();
el.parentNode.replaceChild(newEl, el);
return newEl;
}(newRow));
And finally append the input to the row element:最后将输入附加到行元素:
newRow.appendChild(input);
Then you are ready to save your value using attributes:然后您就可以使用属性保存您的值了:
input.dataset.sno = 10;
or或者
input.setAttribute('data-sno', 10);
Or you can save it into the HTML Element:或者您可以将其保存到 HTML 元素中:
input.sno = 10;
function editRecord () {
var sno = this.sno
|| this.getAttribute('data-snow')
|| this.dataset.snow;
...
}
Choose your way to use it or use them all as I did here.选择您使用它的方式或像我在这里所做的那样全部使用它们。 Now you have
sno
available in your function and more important, it can be the JavaScript object you want .现在您的函数中有
sno
可用,更重要的是,它可以是您想要的 JavaScript 对象。
If you want to use your function as defined on the problem, you'll have to do this trick, when you define the onclick handler:如果您想使用在问题上定义的函数,则必须在定义 onclick 处理程序时执行此技巧:
input.onclick = (function(){
var mySno = sno;
return function(){
editR(mySno);
};
}());
here, using the scope, mySno
will save the reference of the value of sno
every time you define the onclick handler.在这里,使用范围,
mySno
将在您每次定义 onclick 处理程序时保存sno
值的引用。 BUT this will not work for objects , so only numbers or strings will work... plus it's a little bit ugly.但是这不适用于 objects ,所以只有数字或字符串才能工作......而且它有点难看。
//when sno is an integer //当sno是整数时
var sno=10; var sno=10;
row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord(" + sno + ");'"; row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord(" + sno + ");'";
//when sno is a character //当sno是一个字符时
var sno='C'; var sno='C';
row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord(\\'"+ sno +"\\');'"; row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord(\\'"+ sno +"\\');'";
editRecord('sno');
is passing the string 'sno', you need to pass the variable like so: editRecord(sno);
正在传递字符串“sno”,您需要像这样传递变量:
editRecord(sno);
Use the below line I have written for you :使用我为您写的以下行:
Code代码
var sno=10;
row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord(\'"+sno+"\');'";
Never you cant use double quat in double quat or single in single.永远不能在双季铵盐中使用双季铵盐或在单季铵盐中使用单季铵盐。 Its not true.
这不是真的。 "xxxxx"yyyyy"xxxx" you can use "xxxxx'yyyyy'xxxxx" Or "xxxxx\\"yyyyy\\"xxxx" Or "xxxx"+"yyyyy"+"xxxx"
"xxxxx"yyyyy"xxxx" 可以使用 "xxxxx'yyyyy'xxxxx" 或 "xxxxx\\"yyyyy\\"xxxx" 或 "xxxx"+"yyyyy"+"xxxx"
var sno=10; var sno=10; row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord('+sno+');'";
row.insertCell(6).innerHTML="input type='button' value='edit' onclick='editRecord('+sno+');'";
You are passing it already with this line: onclick='editRecord(sno);'你已经用这一行传递了它: onclick='editRecord(sno);'
Just remove the ' from sno.只需从 sno 中删除 '。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.