html输入onchange不接受匿名函数

时间:2014-06-05 20:38:57

标签: javascript html model-view-controller input

为什么这不起作用?

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

chrome给我一个错误, 未捕获的SyntaxError:意外的令牌(

Firefox告诉我 SyntaxError:函数语句需要名称

但这确实有用吗?

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

http://jsfiddle.net/ewzyV/

我在问,因为我正在尝试使用和MVC框架将代码注入onchange事件。

4 个答案:

答案 0 :(得分:11)

onchange="(function(){alert('this should work')})()"

答案 1 :(得分:2)

创建内联函数不是一个好习惯。

我会建议像

这样的东西
<html>
 <script>
   function myFunction(){
     alert('hey it works');
   }
 </script>
 <body>
 <input type ='file' id='kmlFiles2' onchange="myFunction();" />
 </body>
 </html>

你也可以考虑写

function init(){
   document.getElementById('kmlFiles2').onclick=function(){alert('this also works');};
}
window.onload=init;

答案 2 :(得分:0)

                                                            בס''ד

使用您的代码段:

    onchange="function(){alert('why does this not work')}()"

存在一个基本问题。我期望从https://www.w3schools.com/jsref/event_onchange.asp开始,Javascript的格式是(而不是):

    object.onchange = function(){myScript};

使用onchange evenet,&#34;对象。&#34;已经是隐式的,所以要使用的预期正确形式是:

    onchange= "function(){alert("Why does this not work?"); return;}"

没有&#34;返回;&#34;声明很少会出现问题但是 - 即便如此 - 我将其包含在内以使功能完整。

我有类似的情况,我正在努力解决,所以我非常感谢你提出这个问题。谢谢你,祝你好运!

我从这个参考开始:

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; }"

似乎&#34;功能()&#34;也隐含着。我测试了它,警报发布了两次。我刚从单一警报开始。既然它对我有用,你可能想把它作为一个暗示如何&#34;应该&#34;希望也为你工作。

使用内联函数测试一段时间后,它们似乎不稳定,以便能够始终如一地工作。所以我最终得到了下面的代码,它对我来说非常有用,只需定义一个函数来完成需要做的事情:

<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>

答案 3 :(得分:0)

你不能从html元素调用匿名函数,所以你可以通过事件处理实现这一点,如:

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