Javascript html输入onchange不';不接受匿名函数

Javascript html输入onchange不';不接受匿名函数,javascript,html,model-view-controller,input,Javascript,Html,Model View Controller,Input,为什么这不起作用 <input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()"> chrome给了我一个错误, 未捕获的语法错误:意外标记( Firefox告诉我SyntaxError:function语句需要一个名称 但这真的管用吗 <input type="file" id="kmlFiles2" multiple onchan

为什么这不起作用

 <input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()">

chrome给了我一个错误, 未捕获的语法错误:意外标记(

Firefox告诉我SyntaxError:function语句需要一个名称

但这真的管用吗

 <input type="file" id="kmlFiles2" multiple onchange="alert('but this does work')">


我这样问是因为我试图使用MVC框架将代码注入onchange事件。

创建内联函数不是一个好做法

onchange="(function(){alert('this should work')})()"
我会建议像这样的东西

<html>
 <script>
   function myFunction(){
     alert('hey it works');
   }
 </script>
 <body>
 <input type ='file' id='kmlFiles2' onchange="myFunction();" />
 </body>
 </html>
有一个基本问题。我希望Javascript的格式是(相反):

对于onchange-evenet,“object.”已经是隐式的,因此预期使用的正确形式是:

    onchange= "function(){alert("Why does this not work?"); return;}"
缺少“return;”语句很少会出现问题,但即使如此,我还是将其包括在内,以使函数完整

我有一个类似的情况,我正在努力解决,所以我非常感谢你提出这个问题。谢谢你,祝你编程顺利

我从这一点开始:

我发现以下代码正在工作:

    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>
旋转木马要显示的帖子数:

您不能从html元素调用匿名函数,因此可以通过如下事件处理来实现:

<input type="file" id="kmlFiles2" multiple>
 <script>
    document.getElementById("kmlFiles2").addEventListener("change", function(){
        alert('This is working')
    })
 </script>

document.getElementById(“kmlFiles2”).addEventListener(“更改”,函数(){
警报('正在工作')
})

^如果你使用一个执行的函数,例如一个IIFE,它应该可以工作^{e.preventDefault();alert('this should');})(event)“
这是最好的答案!我目前正在测试我发布的Firefox 55.0.3 32位64位Windows 10;如果在如何解决这些问题方面有任何问题,我希望尽快提供更新。我喜欢Besiyata Deshmaya:)
    onchange= "function(){alert("Why does this not work?"); return;}"
    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; }"
<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>
<input type="file" id="kmlFiles2" multiple>
 <script>
    document.getElementById("kmlFiles2").addEventListener("change", function(){
        alert('This is working')
    })
 </script>