0

如何遍历值包含从 Jquery.Example 中的 ajax 调用返回的 HTML 的 json 数据:我想用指定的类计算以下数据中的 li 标签数。

我需要一起计算具有 coded_false 和 ITcoded_false 的 li 标签的数量。此外,我应该能够计算 li 标签的单个计数。如 itcoded_true、coded_true、coded_false、itcoded_false 的计数。

请帮帮我。谢谢!

 $.ajax({
     url: ROOT_PATH + "/test_descriptions/get_table_counts/" + id + '.json',
     type: "get",
     data: {
         dataset_id: dataset_id
     },
     success: function (data) {
         data2 = data.coded_true

     }

 });

 }

下面是我从控制器获得的 json 数据的值。

{"coded_true":"<ul class='tp_info'></ul><ul class='tp_info'><li class='coded_true ITcoded_false'>The sldodialogs.responseoptimization.OptimizationSection.optimize\n         method solves the optimization problem using the parallel pool if the\n         UseParallel option is set to 'always'.</li></ul><ul class='tp_info'><li class='coded_true ITcoded_false'>The optimize command throws a warning if the UseParallel option\n         is set to 'always' and the OptimizedModel option is empty.</li><li class='coded_true ITcoded_false'>The optimize command throws an error if the UseParallel option is\n         set to 'always', the OptimizeModel options is not empty, and the model\n         specified by the OptimizeModel option is dirty.</li></ul><ul class='tp_info'><li class='coded_true ITcoded_false'>The patternsearch solver uses the parallel pool to evaluate\n         candidate solutions in the poll and search sets.</li><li class='coded_true ITcoded_true'>The CPU consumption on workers should increase at the same rate\n         as the master CPU when optimization runs with patternsearch solver in the\n         parallel mode. g860087</li></ul><ul class='tp_info'><li class='coded_true ITcoded_false'>The initParallel method is a protected method.</li><li class='coded_true ITcoded_false'>The initParallel method checks for any Simulink models that\n         are used during optimization and makes sure they are not dirty.</li><li class='coded_true ITcoded_false'>The initParallel method initializes the parallel pool workers by\n         calling the parallelsim.setupWorkers utility. The Options.OptimizedModel\n         and Options.ParallelPathDependencies properties are passed to\n         setupWorkers.</li><li class='coded_true ITcoded_false'>The finishParallel method is a protected method.</li><li class='coded_true ITcoded_false'>The finishParallel method restores the MATLABPOOL workers by\n         calling the parallelsim.cleanupWorkers utility.</li></ul>
4

1 回答 1

1

如果您想使用 jQuery 计算某些文本中包含的 li 的数量,您可以通过以下方式进行:

var someHTML = data2 = data.coded_true;

$(someHTML).find("li").length;

考虑jQuery( html [, ownerDocument ] )

于 2013-08-19T20:57:30.417 回答