我想将字符串变量从一个jquery事件传递到另一个jquery事件。 我搜索了一些有关堆栈溢出的问题和解决方案,但是找不到与我面临的问题相关的解决方案。我正在使用jQuery Flask和Neo4j图形数据库使用Jquery和AJAX执行一些后端操作。
以下是我的jquery代码
<script type="text/javascript">
var asstName = "";
$('select#label_Name').change(function(){
asstName = $('#label_Name').val();
console.log(asstName);
$.ajax({
url: '/get_asset',
data: asstName,
type: 'POST',
dataType: 'json',
}).done(function(data){
// console.log(data);
var print = "";
for (i=0; i < data['nodeList'].length; i++){
print += "<tr>" +
"<td>" + data['nodeList'][i]['Asset_Name'] + "</td>" +
"<td>" + data['nodeList'][i]['Asset_Manufacturer'] + "</td>" +
"<td><button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: red;' class='fa fa-trash'></i></button>" +
" " +
"<button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: blue;' class='fa fa-pencil'></i></button>" +
"</td>" +
"</tr>";
}
$('tbody#tBody').html(print);
})
});
$('#button').click(function(){
var delName = $(this).attr('id');
console.log(asstName);
$.ajax({
url: '/deleteNode',
data: JSON.stringify(delName),
dataType: 'json',
type: 'POST'
}).done(function(data){
console.log(data);
})
});
</script>
如您所见,有两个事件是
$('select#label_Name').change
和
$('#button').click
如何将字符串变量“ asstName”传递给事件按钮单击?
答案 0 :(得分:2)
您可以使用此$('#label_Name').val();
来获取点击处理程序中下拉列表的值,而不是访问变量
据我了解,您只需要点击处理程序中下拉菜单的值
答案 1 :(得分:1)
根据我的假设,以下可能是解决方案。
<script type="text/javascript">
var asstName = "";
$('select#label_Name').change(function(){
asstName = $('#label_Name').val();
console.log(asstName);
$.ajax({
url: '/get_asset',
data: asstName,
type: 'POST',
dataType: 'json',
}).done(function(data){
// console.log(data);
var print = "";
for (i=0; i < data['nodeList'].length; i++){
print += "<tr data-assetname="+asstName+">" + // added a new attr
"<td>" + data['nodeList'][i]['Asset_Name'] + "</td>" +
"<td>" + data['nodeList'][i]['Asset_Manufacturer'] + "</td>" +
"<td><button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: red;' class='fa fa-trash'></i></button>" +
" " +
"<button id=" + data['nodeList'][i]['Asset_Name'] + ">" +
"<i style='color: blue;' class='fa fa-pencil'></i></button>" +
"</td>" +
"</tr>";
}
$('tbody#tBody').html(print);
})
});
$('#button').click(function(){ // I assume this is the trash button click function. If yes, It wont work, because it is dynamically created element and you need to use $(document).on("click"). Also if the table has multiple row, then ID is duplicated.
var delName = $(this).attr('id');
asstName = $(this).closest('tr').attr('data-assetname');
console.log(asstName);
$.ajax({
url: '/deleteNode',
data: JSON.stringify(delName),
dataType: 'json',
type: 'POST'
}).done(function(data){
console.log(data);
})
});
</script>
答案 2 :(得分:0)
我认为您可以直接在事件按钮单击中获取asstName的值,而不是将其解析为select事件。
为什么不在按钮单击事件中添加此代码
var asstName = $('#label_Name').val();
答案 3 :(得分:0)
每当您更改选项列表并单击按钮进行预订时,您都可以发出事件(自定义事件)
例如: 让chnageEvent = new CustomEvent('valuechnaged',{详细信息:{ “值”:_val, “ currentVal”:值 }})
document.dispatchEvent(chnageEvent);