javascript ajax Jquery后无法获取隐藏字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20554117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant get value of hidden field after ajax Jquery
提问by Ryan Bauer
I have a page that when a link is clicked it opens a pop up box to insert new charges. When this pop up box opens I use jquery load() function to insert a table with all the current charges (loaded from mysql) from a script on another page called load_charges.php.
我有一个页面,当点击一个链接时,它会打开一个弹出框来插入新的费用。当这个弹出框打开时,我使用 jquery load() 函数从名为 load_charges.php 的另一个页面上的脚本插入一个包含所有当前费用(从 mysql 加载)的表。
The problem I am having is I can not access any of the hidden values from the content I just loaded I use:
我遇到的问题是我无法访问我刚加载的内容中的任何隐藏值我使用:
var charge_id = $(':hidden:first', $(this)).val();
this normally gets me the value of the first hidden element but it does not work if I am trying get the info from the page load from load_charges.php. I will list my code below:
这通常会为我获取第一个隐藏元素的值,但如果我尝试从 load_charges.php 的页面加载中获取信息,则它不起作用。我将在下面列出我的代码:
case_cpanel.php:
case_cpanel.php:
//HTML Table with Form elememt
//带有表单元素的HTML表格
<table>
<tr class="lead_hover">
<td>
<form class="calc" title="Case Expense Calculator" style="cursor:pointer;">
<input type="hidden" id="lead_id" value="21946295" />
<input type="hidden" id="final_id" value="74" />
<input name="order" type="hidden" id="final_id" value="3" />
</form>
</td>
</tr>
<tr class="lead_hover">
<td>
<form class="calc" title="Case Expense Calculator" style="cursor:pointer;">
<input type="hidden" id="lead_id" value="21978679" />
<input type="hidden" id="final_id" value="79" />
<input name="order" type="hidden" id="final_id" value="1" />
</form>
</td>
</tr>
</table>
// Jquery to load popup form box
// Jquery 加载弹出表单框
$(".calc").click(function () {
var value = $(':hidden:eq(0)', $(this)).val();
$('input[name=lead_id]').val(value);
var value = $(':hidden:eq(1)', $(this)).val();
$('input[name=final_id]').val(value);
var value = $(':hidden:eq(2)', $(this)).val();
$('.order').val(value);
var lead_id = $(':hidden:eq(0)', $(this)).val();
var string1 = "token=<? echo $_SESSION['token']; ?>&lead_id=";
var url = "../ajax/load_charges.php";
var datastring = string1.concat(lead_id);
$('#calc_right_display').html('<div><img src="../imgs/loading4.gif" align="center" /></div>').load(url, datastring).show();
$("#calc_div").overlay().load();
});
load_charges.php:// table that is load with new form elements
load_charges.php:// 加载新表单元素的表格
<table width="266" border="0" cellpadding="5">
<? do { ?>
<tr>
<td width="163">
<? echo $row_charges[ 'rows'][ 'title']; ?>
</td>
<td width="83">$
<? echo $row_charges[ 'rows'][ 'charge']; ?>
</td>
<td width="27">
<form class="delete_charge" title="Delete Charge" style="cursor:pointer;">
<input type="hidden" id="id1" value "<? echo $row_charges['rows']['id']; ?>">
</form>
</td>
</tr>
<? } while ($row_charges[ 'rows']=m ysql_fetch_assoc($row_charges[ 'query'])); ?>
</table>
//Jquery Code on load_charges.php
//在load_charges.php上的Jquery代码
<script>
$(".delete_charge").click(function () { * *
var charge_id = $(':hidden:first', $(this)).val(); * *
var str2 = "token=<? echo $_REQUEST['token']; ?>&delete_charge=true&id=";
var str_lead_id = "&lead_id=<? echo $_REQUEST['lead_id']; ?>";
var url2 = "../ajax/cm_expenses_delete.php";
var datastring2 = str2.concat(charge_id, str_lead_id);
$('#calc_right_display').html('<div><img src="../imgs/loading4.gif" align="center" /></div>').load(url2, datastring2).show();
});
</script>
The problem is var charge_id = $(':hidden:first', $(this)).val(); is not returning any value from the new page that just loaded. Any Help would be greatly appreciated.
问题是 var charge_id = $(':hidden:first', $(this)).val(); 没有从刚加载的新页面返回任何值。任何帮助将不胜感激。
Thank you in advance. Ryan
先感谢您。瑞安
回答by KernelSanders213
You may be having a problem retrieving the value because you are missing an equal sign.
This:<input type="hidden" id="id1" value"<? echo $row_charges['rows']['id']; ?>">
Change to:<input type="hidden" id="id1" value="<? echo $row_charges['rows']['id']; ?>">
This would explain it because your input has no value.
您可能在检索值时遇到问题,因为您缺少等号。
这:<input type="hidden" id="id1" value"<? echo $row_charges['rows']['id']; ?>">
更改为:<input type="hidden" id="id1" value="<? echo $row_charges['rows']['id']; ?>">
这将解释它,因为您的输入没有价值。
回答by Hackerman
I made the following test, based on your data:
我根据您的数据进行了以下测试:
$(document).ready(function(){
var charge_id = $('input:hidden:first').val();
alert(charge_id);
var charge_id = $('input[type=hidden]:first').val();
alert(charge_id);
});
Working fiddle here: http://jsfiddle.net/robertrozas/c4AHM/1/
在这里工作小提琴:http: //jsfiddle.net/robertrozas/c4AHM/1/