בס''ד
使用您的代码段:
onchange="function(){alert('why does this not work')}()"
有一个基本问题。我希望https://www.w3schools.com/jsref/event_onchange.asp Javascript 的格式是(而不是):
object.onchange = function(){myScript};
使用 onchange 事件,“对象”。已经是隐含的,所以预期的正确使用形式是:
onchange= "function(){alert("Why does this not work?"); return;}"
没有“回报”;语句很少给出问题,但 - 即使如此 - 我将其包括在内以使功能完整。
我遇到了类似的情况,正在尝试解决,因此非常感谢您提出这个问题。谢谢你,祝你编程好运!
我从这个参考开始:
https://www.w3schools.com/js/js_function_definition.asp
After a function expression has been stored in a variable, the
variable can be used as a function:
Example
var x = function (a, b) {return a * b};
var z = x(4, 3);
我发现以下代码可以正常工作:
onchange="{alert('Hello 10'); alert('Hello 20'); return; }"
似乎“function()”也是隐含的。我对其进行了测试,并发布了两次警报。我从一个警报开始。由于它对我有用,因此您可能希望以此作为提示,希望它“应该”如何对您也有用。
在使用内联函数测试一段时间后,它们似乎不稳定,以使某些东西始终如一地工作。所以我最终得到了下面的代码,它对我很有效,只需定义一个函数来完成需要做的事情:
<td><strong>Number of Posts For the Carousel to Show:</strong></td>
<td style="text-align: center; vertical-align: middle; padding: 10px;" colspan="2">
<input id="<?php echo esc_attr( $this->get_field_id( 'ceupoststoshow' ) ); ?>" style="vertical-align: middle; horizontal-align: middle;" max="20" min="1" step="1" type="range" value="<?php echo esc_attr( $titleooi ); ?>" onchange="updatetwopoststoshowtext(this.value, this.id, 'ceupoststoshow' , 'titleooi','ceupoststoshowtext')" >
<script type="text/javascript">
function updatetwopoststoshowtext(thistextvalue, thisfieldid, thisoldfromid, thisnewtoidone, thisnewtoidtwo) {
updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid , thisnewtoidone);
updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid , thisnewtoidtwo);
}
</script>
</td>
<td>
<input style="width: 90%;" type="text" id="<?php echo esc_attr( $this->get_field_id( 'ceupoststoshowtext' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'ceupoststoshowtext') ); ?>" type="text" value="<?php echo esc_attr( $titleooi ); ?>" onchange="updatepoststoshowtext(this.value, this.id, 'ceupoststoshowtext' , 'titleooi')" >
<script type="text/javascript">
function updatepoststoshowtext(thistextvalue, thisfieldid, thisoldfromid, thisnewtoid) {
var strthistextvalue= String(thistextvalue); var strthisfieldid = String(thisfieldid);
var strthisoldfromid=String(thisoldfromid); var strthisnewtoid = String(thisnewtoid);
alert ("UPST hello there! strthistextvalue = " + strthistextvalue + " strthisfieldid = " + strthisfieldid + " strthisoldfromid = " + strthisoldfromid + " strthisnewtoid = " + strthisnewtoid );
var subfieldidlength = strthisfieldid.length - strthisoldfromid.length;
var substrfieldid = strthisfieldid.substring (0, subfieldidlength);
alert ("UPST hello there two! subfieldidlength = " + subfieldidlength + " substrfieldid = " + substrfieldid);
var newstrfieldid = substrfieldid + strthisnewtoid;
alert ("UPST hello there three one! newstrfieldid = " + newstrfieldid);
var newElementId = document.getElementById(newstrfieldid);
alert ("UPST hello there three two! newElementId.value = " + newElementId.value);
(document.getElementById(newstrfieldid)).value = strthistextvalue;
alert ("UPST hello there three! (document.getElementById(newstrfieldid)).value = " + strthistextvalue);
alert ("document.getElementById (newstrfieldid = " + newstrfieldid + " ) . value = thistextvalue = " + thistextvalue);
return;
}
</script>