HTML输入onchange不接受匿名函数
html input onchange doesn't accept anonymous function
为什么不工作?
<input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()">
chrome给我一个错误,Uncaught SyntaxError: Unexpected token (.
Firefox告诉我SyntaxError: function statement需要一个name。
但这确实有效吗?
<input type="file" id="kmlFiles2" multiple onchange="alert('but this does work')">
http://jsfiddle.net/ewzyV/我问,因为我是试图使用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>
你也可以考虑写
function init(){
document.getElementById('kmlFiles2').onclick=function(){alert('this also works');};
}
window.onload=init;
你不能从HTML元素中调用匿名函数,所以你可以通过这样的事件处理来实现:
<input type="file" id="kmlFiles2" multiple>
<script>
document.getElementById("kmlFiles2").addEventListener("change", function(){
alert('This is working')
})
</script>
font - family " font - family
与你的代码段:
onchange="function(){alert('why does this not work')}()"
有一个基本的问题。我希望https://www.w3schools.com/jsref/event_onchange.asp的Javascript格式是(而不是):
object.onchange = function(){myScript};
对于onchange事件,"object."已经是隐式的了,所以预期正确的使用形式应该是:
onchange= "function(){alert("Why does this not work?"); return;}"
没有"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>
相关文章:
- Ember Data DS.Model's set函数不起作用
- jQuery加载的async和ready函数不工作
- 为什么构造函数不是构造函数
- 为什么我的JavaScript堆栈排序函数不起作用
- 参数更改时,角度表达式中的函数不更新
- autocomplete不接受源的json对象
- PhantomJS不接受传递给page.eevaluate()的函数的参数
- 自定义数组函数不接受参数
- Javascript不接受我的函数参数
- AngularJS ng控制器指令不接受javascript中的变量(作用域函数),也不给出任何错误
- setBackgroundRGB不接受函数参数
- 谷歌图表# 39;addRows()函数不希望接受数组
- HTML输入onchange不接受匿名函数
- javascript函数不接受传入的Object变量
- Parameter需要一个函数,但不接受函数
- 为什么一个函数不接受第二次请求?
- 制作一个node.js模块,该模块要么接受参数,要么不接受参数,并返回一个适当的函数/对象
- Javascript函数不接受JSP创建的字符串
- 为什么javascript中的函数不接受未定义的变量
- jQuery $.ajax 函数不接受我尝试发布的数组