简体   繁体   English

试图更改onchange中的所有数字 - onchange.replace不起作用?

[英]Trying to change all the numbers in an onchange - onchange.replace doesn't work?

I have an xsl page which uses 我有一个使用的xsl页面

 <xsl:variable name="pos" select="Position()"/> 

in the onchange events 在onchange事件中

onchange="updateDropdown({$pos});someElement_{$pos}.value = {$pos}";

When evaluated on page p=load this will be read as 在页面p = load上进行评估时,将其读作

onchange="updateDropdown(1);someElement_1.value = 1";
onchange="updateDropdown(2);someElement_2.value = 2";
onchange="updateDropdown(3);someElement_3.value = 3";

When I add a row to the bottom of this using a button which copies the entire first row to the bottom it had to go through and update these numbers because it is not handled 当我使用一个将整个第一行复制到底部的按钮在其底部添加一行时,它必须通过并更新这些数字,因为它没有被处理

I do 我做

lastRowEl = rowEls[rowEls.length-1];

then 然后

lastRowEl.id = "element_" + rowEls.length
lastRowEl.value = "";

finally, 最后,

lastRowEl.onchange = lastRowEl.onchange.replace(/\d/g, rowEls.length)

id gets changed to element_4 value gets modified to blank id被更改为element_4值被修改为空白

but onchange.replace does not work and so onchange remains as 但onchange.replace不起作用,因此onchange保持不变

onchange="updateDropdown(1);someElement_1.value = 1";

instaead of instaead

onchange="updateDropdown(4);someElement_4.value = 4";

How can I replace all numbers in an onchange function and reset the onchange function with the numbers modified for the element in the last row? 如何替换onchange函数中的所有数字并使用为最后一行中的元素修改的数字重置onchange函数?

Thanks 谢谢

This seems a bit weird and I wonder why it works at all, since the value returned by your lastRowEl.onchange is most likely a Function and not a String. 这看起来lastRowEl.onchange ,我想知道它为什么会起作用,因为你的lastRowEl.onchange返回的值很可能是一个函数,而不是一个字符串。 Some automatic toString() call seems to happen on your platform (doesn't work when I test this in Safari). 一些自动toString()调用似乎发生在你的平台上(当我在Safari中测试时不起作用)。 However, the result will most likely be a multiline string and you will probably need to use multiline RegExps, eg /\\d/mg . 但是,结果很可能是多行字符串,您可能需要使用多行RegExps,例如/\\d/mg

Even if this works it is pretty ugly. 即使这样做也很难看。 What about defining a function which does all the calls you put into your handler? 如何定义一个函数来完成你在处理程序中的所有调用?

function onchangeHandler(index) {
    updateDropdown(index);
    window["someElement_" + index].value = index;
}

You would then assign these handler functions in HTML 然后,您将在HTML中分配这些处理函数

< ... onchange="onchangeHandler(1);" .... />

and update them in Javascript 并在Javascript中更新它们

(function() {
    var index = rowEls.length;
    lastRowEl.onchange = function() {onchangeHandler(index);};
})();

Note that the closure around the assignment is may not be necessary depending on your surrounding code. 请注意,根据您的周围代码,可能不需要围绕分配的闭包。 You will most likely need the explicit index variable definition, though. 但是,您很可能需要显式索引变量定义。

Try the following solution 尝试以下解决方案

var onch = new String(lastRowEl.onchange); 
onch = onch.replace(/\d/g, rowEls.length);

lastRowEl.onchange = new Function(onch);

This will work as you expect. 这将按预期工作。

Hope this solves your problem. 希望这能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM