javascript - 在 CakePHP 中使用 focus() 和 onload()

标签 javascript cakephp-1.3

有没有一种方法可以让我们将焦点设置到 CakePHP 中的特定文本框。就像我们在谷歌主页上的文本框一样。我们加载页面并开始输入。是否可以在 Cake 中实现相同的效果。

cake 是否支持 javascript 的 onload 函数。我只需要它用于我的 View ,并且不想为此包含外部文件。我有一个用户定义的函数,可以正常工作,但 onload 不起作用。

最佳答案

只需在 head 标签之间使用 JavaScript,如下所示

<script type="text/javascript">
    window.onload=function(){
        document.getElementsByName('txt1')[0].focus(); // txt1 is the name of textbox
    } ​ 
</script>

更新:如果您只想将其用于特定页面,则可以将其保留在该 View 中,在这种情况下,您只需将其粘贴到 HTML 之后即可> 代码如下

<script type="text/javascript">
        document.getElementsByName('txt1')[0].focus(); // txt1 is the name of textbox
</script>

关于javascript - 在 CakePHP 中使用 focus() 和 onload(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11068405/

相关文章:

javascript - 如何在 Javascript 中检查脚本是否作为 ES6 模块运行(以便它可以 `export` )?

javascript - 我的函数似乎只返回未定义的值

cakephp - 如何更改分页条件?

php - CakePHP 1.3 还是 2.0.0-RC2?

php - 在 CakePHP 中提交当前时间戳

cakephp - 如何本地化 CakePHP 插件?

javascript - HTML 详情标签开启方向?如何改变?

javascript - 根据 woocommerce 中的产品价格显示百分比

javascript - 如何在 HTML 表单中捕获键盘输入

cakephp - 如何在cakephp中的另一个 Controller 中调用Controller Action ?