获取HTML输入到jquery函数

时间:2015-07-27 16:50:21

标签: javascript jquery html

我正在尝试将HTML输入变量转换为javascript函数,并且无法弄清楚如何执行此操作。 javascript适用于TimeMap。相关的HTML如下所示:

<input type="text" id="keyword_search" class="form-control">
<input type="button" class="btn btn-primary" value="Search" onClick="getResult()">

到目前为止我的javascript看起来像这样。

function getResult(){
    var keyword = $("#keyword_search").val();
    return(keyword);
}

$(function () {

    var keyword = getResult();

    tm = TimeMap.init({
        mapId: "map",               // Id of map div element (required)
        timelineId: "timeline",     // Id of timeline div element (required)
        options: {
            eventIconPath: "../static/images/"
        },
        datasets: [
            {
                title: "JSON String Dataset",
                type: "custom",

                options: {
                    // json file
                    url: "articles/filter/?keyword=" + keyword
                }
            }
        ],

        bandIntervals: [
            Timeline.DateTime.MONTH,
            Timeline.DateTime.YEAR
        ]
    });
});

基本上我需要将HTML输入中的关键字放到网址的末尾。然后这将返回一些JSON。任何帮助非常感谢,如果您需要更多信息,请告诉我。谢谢!

UPDATE!整个javascript!

function getResult(){
    var keyword = $("#keyword_search").val();
    return(keyword);
}

    $(function () {

        TimeMap.loaders.custom = function (options) {
            var loader = new TimeMap.loaders.remote(options);
            loader.parse = JSON.parse;
            loader.preload = function (data) {
                return data["results"]
            };
            loader.transform = function (data) {
                console.log(data);
                return {
                    "title": data.text,
                    "start": data.date,
                    "options": {
                        "description": data.bicyclistCount
                    },
                    "point": {
                        "lat": data.Lat,
                        "lon": data.Lon
                    }
                };
            };
            return loader;
        };

        $("select[type=button]").click(function () {
            var keyword = getResult();



            tm = TimeMap.init({
                mapId: "map",               // Id of map div element (required)
                timelineId: "timeline",     // Id of timeline div element (required)
                options: {
                    eventIconPath: "../static/images/"
                },
                datasets: [
                    {
                        title: "JSON String Dataset",
                        type: "custom",

                        options: {
                            // json file
                            url: "articles/filter/?keyword=" + keyword
                        }
                    }
                ],

                bandIntervals: [
                    Timeline.DateTime.MONTH,
                    Timeline.DateTime.YEAR

                ]
                });
            });
        });

        SimileAjax.History.enabled = false;

2 个答案:

答案 0 :(得分:0)

我认为问题在于你是如何尝试触发获取的,因为你只有OnAuthenticated本身没有做任何事情(它只返回输入的值)。相反,您应该在点击提交按钮时加载地图:

const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
var opt = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
    AppId = "...",
    AppSecret = "...",
    SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
    Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
        {
            // context.User only have two keys: name and id, email is not there...

            context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
            foreach (var x in context.User)
            {
                var claimType = string.Format("urn:facebook:{0}", x.Key);
                string claimValue = x.Value.ToString();
                if (!context.Identity.HasClaim(claimType, claimValue))
                    context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));

            }
            return Task.FromResult(0);
        }
    }
};
opt.Scope.Add("email");
app.UseFacebookAuthentication(opt);

答案 1 :(得分:0)

使用新的JS,您的getResult()函数不是很有用。 在按钮上添加一个ID:

<input type="button" class="btn btn-primary" value="Search" id='search'>

并更改点击功能:

$("#search").click(function () {
    var keyword = $("#keyword_search").val();
    ...
}

另外,我假设您在全球范围内创建了tm变量?