在每个函数

时间:2016-12-11 00:24:47

标签: javascript jquery json

我有一个JSON文件,js / links.json:

{ "allLinks": [
 {
   "Link": "http://bbc.com",
   "Poster": "John"
 },
 {
   "Link": "http://espn.com",
   "Poster": "Jane"
 }
...etc.

以及以下HTML:

<input placeholder="http://" id="urlField" type="text">
<button type="button" id="search" name="button">Search</button>
<div id="result"></div>

我想要的行为是:

  • 您输入了输入
  • 的链接
  • 单击该按钮时,获取links.json数组并循环显示它。
  • 如果链接存在,则输出到#result:&#34;这已存在,并由[海报]发布&#34;
  • 如果它不存在,则输出到`#results&#34;此链接不存在。&#34;

这就是我现在尝试这样做的方式:

    $(function(){

    $.ajax({
        type : 'GET',
        url : 'js/links.json',
        async : false,
        beforeSend : function(){/*loading*/},
        dataType : 'json',
        success : function(jsonResult){

                $("#search").click(function() {
                    var searchValue = document.getElementById('urlField').value;

                    // Go through the whole list and find the Link name
                    var linkExists = false;
                    $.each(jsonResult.allLinks, function(i, obj) {
                            if (obj.Link === searchValue ) {
                                linkExists = true;
                                alert("It exists"); 
                                return false;
                            }
                            else {
                                linkExists = false;
                                alert("It doesn't exist");
                                return false;
                            }
                    });

                });

        }
    });
});

这里肯定有问题,因为如果我使用else,它似乎有效。当我添加它时,它并不总是默认为&#34;它不存在。&#34;

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

var quadRowIndex = function (diameter, quadNumber) { //diameter should be a positive even number //quadNumber should be between 0 and index of last number in last row (inclusive) var quadIndex = 0; //holds the RowIndex, which the function will return once the row contains quadNumber var rowStartNum = 0; var rowLength = 2; //iterate through first half while (rowLength <= diameter) { rowStartNum += rowLength; if (rowStartNum > quadNumber) { return quadIndex; } quadIndex++; rowLength += 2; } rowLength -= 2; //iterate through second half if still here while (rowLength >= 2) { rowStartNum += rowLength; if (rowStartNum > quadNumber) { return quadIndex; } quadIndex++; rowLength -= 2; } //still here -- number was too high, return error signal return -1; }; console.log(quadRowIndex(6, 9)); console.log(quadRowIndex(6, 20)); console.log(quadRowIndex(6, 100));语句的内容拉出for循环,否则每次点击第一个元素时else都会失败并且不会产生匹配。

alert
$(function() {

      $.ajax({
        type: 'GET',
        url: 'js/links.json',
        async: false,
        beforeSend: function() { /*loading*/ },
        dataType: 'json',
        success: function(jsonResult) {

          $("#search").click(function() {
            var searchValue = document.getElementById('urlField').value;

            // Go through the whole list and find the Link name
            var linkExists = false;
            $.each(jsonResult.allLinks, function(i, obj) {
              if (obj.Link === searchValue) {
                linkExists = true;
                alert("It exists");
                return false;
              }

            });
            if (!linkExists) {
                alert("It doesn't exist");
                return false;
            }

          });

        }
      });
    });

相关问题