【发布时间】:2014-08-18 22:28:06
【问题描述】:
我有一个名为“getData”的函数,其中包含一个通过 json 成功返回我的数据(动态创建的水果列表)的 ajax 调用。我有另一个名为“AddFruit”的函数,当用户单击将文本值输入到文本框中的按钮并且该值与我的 json 数据中包含的值之一不匹配时,它应该检查一个复选框。
例如,如果从 ajax 调用返回的数据是 {'orange', 'apple', 'pear'} 并且用户单击了将“banana”添加到文本框的按钮,则应选中我的复选框。
以下是我目前尝试过的:
function AddFruit(data) {
var fruit = $("#Fruit").val(); //the value of input text box that gets populated when a user clicks '#ButtonAdd' button.
$("#ButtonAdd").click(function() {
$.each(data, function (item) {
if ($('fruit:contains(" + data[item] + ")'))
{
$("#Oldform").prop ("checked", false);
} else
{
$("#Oldform").prop ("checked", true);
}
});
});
}
getData().done(AddFruit); // when the ajax call is successfully completed, run the AddFruit function.
【问题讨论】:
-
@AdamMerrifield 空间在这里无关紧要...
-
@AdamMerrifield -- 他与
prop和 (.) 之间的空格保持一致。如果这是他想这样做的方式,那对我来说很好。这也让我很烦恼 - 但我有我的自己的怪癖,并不会增加对话。对他来说可能更容易阅读。耸耸肩