我有一个名为“ getData”的函数,其中包含一个ajax调用,该调用通过json成功返回了我的数据(动态创建的水果列表)。我有另一个名为“ 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.
要搜索从ajax调用返回的数据,需要一个带有break语句的for循环。当数据中的元素与文本输入匹配时,for循环退出。
var ajaxData = null;
function AddFruit(data){
ajaxData = data;
$("#Button").click(function(){
for (var i = 0; i < data.length; i++){
if (data[i] == $("#Fruit").val())
{
//leave check box unchecked
break;
}
else{
//check check box
}
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句