2015-11-03 130 views
0

我在爲我的項目使用genderAPIjQuery
我想獲得#name的性別結果,但好像我的代碼根本不認識它(沒有alert正在跳出)。有人會建議你爲什麼好嗎?jQuery:從性別獲得結果API

他們提供的jQuery plugin例子如下:

$('input#firstname').genderApi({key: genderApiClientKey}).on('gender-found', function(e, result) { 

    if (result.accuracy >= 60) {  
    alert('Gender found: ' + result.gender); 
    } 

}); 

下面是我的嘗試,你會發現完整的代碼here

HTML:

<head> 
    <script type="text/javascript" src="https://gender-api.com/js/jquery/gender.js"></script> 
</head> 

<body> 
    <section id="fetch"> 
     <input type="text" placeholder="Please enter your name here" id="term" /> 
     <button type="button" id="search">ENTER</button> 
    </section> 

    <div id="name"> 
     <div id="nameBox"> 
     </div> 
     <div id="nameText"> 
     </div> 
    </div> 
</body> 

的jQuery:

var apiUrl = "https://sheetsu.com/apis/f924526c"; // this is another API I created on Google Spreadsheet 

$.getJSON(apiUrl, function (json) { 

    console.log(JSON.stringify(json)); 
    var item = json.result.find(function (e) { 

     return e.name == content; 

    }) || json.result[0]; 
    console.log("PRINT ID: " + item.id); 


    var name = item.name || content; 
    var $nameText = $("#nameText"), 
     str = name; 
    html = $.parseHTML(str), 
     nodeNames = []; 
    $nameText.append(html); 
    console.log("Name: " + name); 

    // genderAPI here to get gender result 
    $('name').genderApi({ 
     key: "oyxLJkutVSYXWNVzGP" 
    }).on('gender-found', function (e, result) { 
     if (result.accuracy >= 60) { 
      alert('Gender found: ' + result.gender); 
     } 
    }); 
}); 

回答

0

您是否正在嘗試爲以下文本字段執行genderAPI?

<input type="text" placeholder="Please enter your name here" id="term" /> 

那麼你的代碼應該是這樣的:

$('input#term').genderApi({ 
     key: "oyxLJkutVSYXWNVzGP" 
    }).on('gender-found', function (e, result) { 
     if (result.accuracy >= 60) { 
      alert('Gender found: ' + result.gender); 
     } 
    }); 
+0

感謝很多:)))× –