我有一个非常简单的问题,但我无法弄清楚, 我对此是新手。
我有以下功能,用户将单击他/她想要搜索的内容的按钮,然后当他/她选择并单击选项周围的内容时,他/她将单击搜索按钮进行搜索。
这是我从 anchor 标记获取值的代码,选择包含在 anchor 标记中。
$(document).on('click' , '#area_id_1' , function(){
var getText1 = $("#area_id_1").text();
alert(getText1); return false; });
$(document).on('click' , '#area_id_2' , function(){
var getText1 = $("#area_id_2").text();
alert(getText1);
return false;
});
$(document).on('click' , '#area_id_3' , function(){
var getText1 = $("#area_id_3").text();
alert(getText1); return false;});
$(document).on('click' , '#area_id_4' , function(){
var getText1 = $("#area_id_4").text();
alert(getText1);
return false;
});
$(document).on('click' , '#area_id_5' , function(){
var getText1 = $("#area_id_5").text();
alert(getText1); return false;});
$(document).on('click' , '#area_id_6' , function(){
var getText1 = $("#area_id_6").text();
alert(getText1); return false; });
$(document).on('click' , '#area_id_7' , function(){
var getText1 = $("#area_id_7").text();
alert(getText1); return false; });
$(document).on('click' , '#area_id_8' , function(){
var getText1 = $("#area_id_8").text();
alert(getText1); return false; });
这是我的搜索按钮功能 //搜索功能按钮
$(document).on('click', '#btnSearch', function() {
}
问题是,我如何能够存储从 anchor 标记获得的值,以便在单击 btnSearch 函数时可以使用它?
最佳答案
首先定义全局变量 getText1,如下所示。
var getText1;
$(document).on('click' , '#area_id_1' , function(){
getText1 = $(this).text();
return false;
});
...(rest of your code)
// and you will get the variable in below function
$(document).on('click', '#btnSearch', function() {
alert(getText1); // you can check here if it is empty or not
}
关于javascript - 如何从 anchor 标记获取值并将其用于另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759149/