在onClick上调用JQuery函数

时间:2017-08-23 20:27:29

标签: javascript jquery

我有一个显示隐藏div标签的JQuery函数。所以现在它一旦文档加载就会起作用但我想只在单击一个按钮时生效。我尝试创建一个函数并将其命名为hello,其中包含相同的JQuery代码,但它不起作用。

JQuery函数

$("#Signers").show(30,function() {
 }); 

按钮

<a href="#x" onclick ="validateEmailAddress();hello()"><span>Confirm</span></a>

请指导我。

...谢谢

3 个答案:

答案 0 :(得分:1)

只需将jQuery函数包装在JavaScript函数中,并确保您实际拥有ID为git rebase的元素,并且您的代码将起作用。

另外,不要忘记包含jQuery。

工作示例:

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Deployer
{
    public class RecursiveFolderUploader
    {
        private const string SrcFullPath = @"C:\prg\Snowinmars\Snowinmars.Ui";
        private readonly IList<string> foldersOnServer;
        private Client ftpClient; // that's your ftp client, init it

        public RecursiveFolderUploader()
        {
            this.foldersOnServer = new List<string>();
        }

        private void UploadAll(DirectoryInfo directoryInfo)
        {
            // ftp://login:password@127.0.0.1/path/to/root/mydir/anotherdir/file.dat
            // ^________________uri_______________________^_relevationPath_^
            foreach (var file in directoryInfo.EnumerateFiles())
            {
                if (!file.Directory.FullName.StartsWith(RecursiveFolderUploader.SrcFullPath))
                {
                    throw new InvalidOperationException($"File {file.FullName} is not from {RecursiveFolderUploader.SrcFullPath} folder");
                }

                string relevationPath; // all folders from root up to file

                if (file.Directory.FullName.Length == RecursiveFolderUploader.SrcFullPath.Length)
                {
                    relevationPath = "";
                }
                else
                {
                    relevationPath = file.Directory.FullName.Substring(RecursiveFolderUploader.SrcFullPath.Length, file.Directory.FullName.Length - RecursiveFolderUploader.SrcFullPath.Length);

                    if (relevationPath.StartsWith("\\"))
                    {
                        relevationPath = relevationPath.Remove(0, 1);
                    }
                }

                string destination;
                if (string.IsNullOrWhiteSpace(relevationPath))
                {
                    destination = file.Name;
                }
                else
                {
                    destination = Path.Combine(relevationPath, file.Name).Replace("\\", "/");
                }

                try
                {
                    ftpClient.UploadFile(file.FullName, destination);
                }
                catch (WebException e)
                {
                    // that means that there's no such folder or something else goes wrong
                    // we can check it by creating folders and try again
                    var parts = relevationPath.Replace("\\", "/").Split('/');

                    for (int i = 1; i <= parts.Length; i++)
                    {
                        var path = string.Join("/", parts.Take(i));

                        if (!foldersOnServer.Contains(path))
                        {
                            ftpClient.MakeDirectory(path);
                            foldersOnServer.Add(path);
                        }
                    }

                    try
                    {
                        ftpClient.UploadFile(file.FullName, destination);
                    }
                    catch (Exception innerE)
                    {
                        // if it doesn't help - trouble isn't in folders
                        throw new WebException($"Can't find folder {relevationPath}", innerE);
                    }
                }
            }

            foreach (var directory in directoryInfo.EnumerateDirectories())
            {
                UploadAll(directory);
            }
        }
    }
}
&#13;
Signers
&#13;
function validateEmailAddress() {
  console.log('Email');
}

function hello() {
  console.log('JavaScript');
  
  $("#Signers").show(30, function() {
    // do something after show
    console.log('jQuery');
  });
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你的意思是当文档加载你的div标签时(带有id&#34; Singers&#34;)将被隐藏。当单击一个按钮时,将显示div。不是吗?

如果是,那么正确的方法如下:

1.add style

<style>
#Signers{display:none;}
</style>

2.add js

<script>

    $(document).ready(function(){
        //jquery code
    });
    function hello(){
        $("#Signers").show(30,function() {}); 
    }

</script>

答案 2 :(得分:-1)

我终于解决了。我知道在上面的答案中函数正在按预期工作,但在我的情况下,它们都没有通过将JQuery代码放在JS函数中来工作。我做了什么,我宣布了ready函数,它基本上调用了JS函数和onClick事件,函数按需运行。

function hello(){
 $("#Signers").show(30,function() {
 }); 
}

$(document).ready(function () {
    hello();
  });