【问题标题】:store value in JSON on button click单击按钮时将值存储在 JSON 中
【发布时间】:2013-09-03 13:56:30
【问题描述】:

我是 JSON 新手,我正在尝试使用 JSON 保存数据。当我们单击按钮时,我有一个带有一些按钮的元素列表,我希望按钮的相应值保存在 JSON 中。我还想将标题与 JSON 中已经存在的标题进行比较。

演示Here

【问题讨论】:

  • 您的意思是要将当前单击的按钮的标题与之前保存的标题进行比较?
  • 您的示例与 JSON 无关,而是关于数组和 object initializers,也称为对象文字。
  • json部分在哪里?
  • 是的,我想将标题与 json 上已经存在的标题进行比较

标签: javascript jquery html json


【解决方案1】:

您可以简单地使用for 循环来检查具有该标题的元素是否已经存在:

function alreadyAdded(itemTitle) {
    for (var i = 0; i < objArray.length; i++) {
        if (objArray[i].title === itemTitle) {
            return true;
        }
    }
    return false;
};

另外,您使用的不是 json 对象,而是一个 JavaScript 数组。

Demo

【讨论】:

  • 这里的objectArry是什么
  • @Subhash 它是一个对象数组。看演示。
  • 谢谢它的工作,现在我想以表格的形式在其他页面中检索这个 JSON 值,这可行吗
【解决方案2】:

试试这个 http://jsfiddle.net/Z3v4g/

var counter = 0;
var jsonObj = []; //declare object

    $('.imgbtn').click(function () {

        var title = $(this).parent().parent().find('span').html();
        var image = $(this).parent().parent().find('img').prop('src');

        for( var i=0; i<jsonObj.length; i++){
           if( jsonObj[i].title == title ) return false;
        };

        counter++;
        $('#lblCart').html(counter);

        jsonObj.push({
           id: counter,
           title: title,
           image: image,
           description: 'Example'
           });
        });

【讨论】:

    【解决方案3】:

    我假设您想将值存储在数组中,并且在单击按钮期间您想检查该项目是否已存在于数组中。如果这是真的,那么您可以使用以下代码 -

    var counter = 0;
    var jsonObj = []; //declare object 
    
    $('.imgbtn').click(function () {
        var title = $(this).parent().parent().find('span').html();
        var image = $(this).parent().parent().find('img').prop('src');
        var match = $.grep(jsonObj, function (e) { 
            return e.title == title; 
        });
    
        if (match.length > 0) {
            // This title already exists in the object.
            // Do whatever you want. I am simply returning.
            return;
        }
    
        counter++;
        $('#lblCart').html(counter);
        jsonObj.push({
            id: counter,
            title: title,
            image: image,
            description: 'Example'
        });
    });
    

    请注意,我已经在回调函数之外声明了数组。这确保了回调的所有调用都在同一个数组对象上进行。在回调中声明它只是使它可用于单个回调调用。

    还请注意,您只是使用数组来存储普通的 JavaScript 对象。

    Demo.

    【讨论】:

      【解决方案4】:

      第一:

      var jsonObj = []; //declare object
      

      这不是 JSON。这是一个数组。 JSON 只是 Javascript 对象的表示法。要声明一个对象,您应该这样做:

      var jsonObj = {};
      

      或:

      var jsonObj = new Object();
      

      在此之后,您可以按照您的要求执行此操作:

      var counter = 0;
      var jsonObj = new Object();
      
      $('.imgbtn').click(function () {
         var title = $(this).parent().parent().find('span').html();
         var image = $(this).parent().parent().find('img').prop('src');    
         if (!(title in jsonObj)) { // if item is not in the object, (title in jsonObj) returns true of false
             jsonObj[title] = { // When you have hundreds of items, this approach is way faster then using FOR loop, and if you need to alter the item or get one value, you can just call it by name: jsonObj['ABC'].image will return the path of the image
                 id: counter,
                 image: image,
                 description: 'Example'
             }
             counter++;
             $('#lblCart').html(counter);
         } else {
             // Do what you want if the item is already in the list
             alert('Item already in the list');
             console.log(jsonObj[title]);
         }
      });
      

      不要使用 FOR 循环来做你不想做的事情,如果计数器变高,它只会减慢你的应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-17
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        相关资源
        最近更新 更多