2017-07-18 25 views
0

我想使用java脚本为header中的按钮创建一个处理程序。下面我视图模型给出:如何为odoo 10中的按钮点击事件添加一个java脚本处理程序?

 <template id="assets_backend" name="petstore" 
     inherit_id="web.assets_backend"> 
     <xpath expr="." position="inside"> 

    <script type="text/javascript" 
     src="/mypetstore/static/src/js/model_access.js"> 
    </script> 

    <link href="/mypetstore/static/src/css/petstore.css" 
     rel="stylesheet"> 
    </link> 
     </xpath> 
    </template> 



    <record model="ir.ui.view" id="my_pet_store_form"> 
     <field name="name">my_pet_store_form</field> 
     <field name="model">petstore.message</field> 
     <field name="type">form</field> 
     <field name="arch" type="xml"> 
     <header> 
     <button name="click_me" string="Click" 
       class="oe_highlight"/> 
     </header> 
      <form string="Message of the day"> 

       <group col="2"> 
        <group> 
         <field name="data"/> 
        </group> 
       </group> 
      </form> 
      </field> 
     </record> 

当“click_me”按钮,用户点击,然后调用一个简单的JavaScript函数或操作客户端。只需打印警报。 JS部分: odoo.define( 'mypetstore.model_access',函数(要求){ “使用严格”; VAR类=要求( 'web.Class'); VAR的widget =要求( 'web.Widget' ); VAR核心=需要( 'web.core'); VAR utils的要求=( 'web.utils');

jq('#click_me').bind('click', function(){ 
     alert("hello"); 
     }); 
    }); 
+0

这是 “JavaScript的”,而不是 “Java脚本”。 – 2017-07-18 13:27:32

回答

0

这是怎么了你的按钮应该的样子,只是增加onclick哗哗就是你处理器

  <header> 
      <button onclick="myFunction()" name="click_me" string="Click" 
       class="oe_highlight"/> 
     </header> 

myFunction()将是您的JavaScript代码。现在

报警代码将是这样的:

<script type = "text/ javascript"> 
    function myFunction() { 
    alert("Hello! I am an alert box!!"); 
    } 
</script> 

希望它帮助。

1

首先,我建议你给一些ID属性给对象,你想使用。 像

<button name="click_me" id="click_me" string="Click" 
       class="oe_highlight"/> 

JQuery的:

jq('#click_me').bind('click', function(){ 
    alert("hello"); 
}); 
+0

获取错误:“错误:某些模块无法启动” – NidhinMohanCheriyan

+0

我在我的js文件中添加了这段代码,并收到错误消息。我已经用上面的代码编写了我的js代码 – NidhinMohanCheriyan

+0

使用$来代替,我已经将jQuery对象重命名为jq。很抱歉不便, 相反, JQ( '#click_me') 使用 $( '#click_me') –

相关问题