简体   繁体   English

使用标签将按钮上的javascript函数调用

[英]call a javascript function on button with hashtag

im not familiar with javascript but i want to load a function on a button click. 我不熟悉javascript,但我想在按钮单击时加载功能。

in the head i've got the javascript 在头,我有JavaScript

<script type="text/javascript"> 
    $('#file_upload').uploadify({
        'formData'     : {
            'timestamp' : '1349865153',
            'token'     : 'ac534e1229656a7817ab0f20f9933c23',
            'pid'     : '<?=$idProject?>'
        },
        'swf'      : '<?=base_url();?>uploadify/uploadify.swf',
        'uploader' : '<?=base_url();?>uploadify/uploadify.php',
        'buttonText' : 'Bestand upload',
        'onUploadComplete': function() {location.reload(true)}
    }); 
</script>

but how do i call the javascript with a button? 但是我如何用按钮调用JavaScript?

You can try this, just have 你可以尝试一下

$("#ID").click(function() {
    alert("hello");
    });

Like this. 像这样。

var btn = document.getElementById('myButtonId');
btn.onclick = function(){
  console.log('do this');
}

Obviously replace #myButtonId with whatever the ID is of your button and console.log with whatever code you want to run when the button is clicked. 显然,将#myButtonId替换为按钮的ID,将console.log替换为单击按钮时要运行的任何代码。

here what you have on the head is jQuery code. 这里您头上是jQuery代码。

If you want to use the same jQuery code to achieve this instead of javascript, you can use below code. 如果要使用相同的jQuery代码而非JavaScript来实现此目的,则可以使用以下代码。

<input type="button" id="myBtn" value="Click"/>

jQuery code jQuery代码

$("#myBtn").click(function(){
        $('#file_upload').uploadify({
        'formData'     : {
            'timestamp' : '1349865153',
            'token'     : 'ac534e1229656a7817ab0f20f9933c23',
            'pid'     : '<?=$idProject?>'
        },
        'swf'      : '<?=base_url();?>uploadify/uploadify.swf',
        'uploader' : '<?=base_url();?>uploadify/uploadify.php',
        'buttonText' : 'Bestand upload',
        'onUploadComplete': function() {location.reload(true)}
       });
   });

To get the jQuery work in code you need to include any version of the jQuery.js file in your page.. also you need to put the above jQuery code inside ready function , which is the jQuery document loading call. 要使jQuery在代码中起作用,您需要在页面中包括任何版本的jQuery.js文件。此外,您还需要将上述jQuery代码放入ready函数中,这是jQuery文档加载调用。

Try this, 尝试这个,

<div id="buttonSomeID">Click</div>

$("#buttonSomeID").click(function(){
    $('#file_upload').uploadify({
        'formData'     : {
            'timestamp' : '1349865153',
            'token'     : 'ac534e1229656a7817ab0f20f9933c23',
            'pid'     : '<?=$idProject?>'
        },
        'swf'      : '<?=base_url();?>uploadify/uploadify.swf',
        'uploader' : '<?=base_url();?>uploadify/uploadify.php',
        'buttonText' : 'Bestand upload',
        'onUploadComplete': function() {location.reload(true)}
    });
});

DEMO DEMO

use the on method provided by jquery 使用jquery提供的on方法

$("#mybtn").on("click", function() {
  //your code goes here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM