[英]Can't get value from dynamically created hidden input field
I got an element, dynamically added on the page after an ajax call, that has an hidden input field within: 我得到了一个在ajax调用后动态添加到页面上的元素,该元素在其中具有隐藏的输入字段:
<div id='added_title'>
<b>Title</b>
<input type='hidden' id='title_n' name='title_n' value='TITLE_NAME'/>
</div>
After the ajax call i need to get input field value to show it in another div but without any event or click, just after the ajax call finish. 在ajax调用之后,我需要获取输入字段值以在另一个div中显示它,但是没有任何事件或单击,就在ajax调用完成之后。
i tried: 我试过了:
$("input[name='title_n']").val();
$("input[id='title_n']").val();
$("#title_n").val()
$("#added_title input[name='title_n']").val();
I also tried to use data-attribute like this: 我也尝试使用如下数据属性:
<div id='added_title' data-title = 'TITLE_NAME'>
<b>Title</b>
</div>
accessing the data with: 通过以下方式访问数据:
$('#added_title').data('title');
But i always get "undefined" value. 但是我总是得到“未定义”的值。
Any suggestion? 有什么建议吗?
EDIT 编辑
Ajax call: Ajax呼叫:
$.ajax({
type: 'POST',
url: '/tools/get_title.php',
data: { title: title},
success: function (response) {
$("#title_div").html(response);
}
});
where "title_div" is the ID of "added_title" parent. 其中,“ title_div”是“ parented_title”父级的ID。
$("#title_n").val()
is the fastest and easiest way to get the value that you are after. $("#title_n").val()
是获得所追求的价值的最快,最简单的方法。
The problem with the code you have above, is that the $.ajax
call is fired, then code continues to execute, including, presumably the call to the hidden input. 您上面的代码的问题在于,将触发$.ajax
调用,然后代码继续执行,包括对隐藏输入的调用。
Then the $.ajax
call completes and fires the success event, which puts said element on the page. 然后$.ajax
调用完成并触发成功事件,该事件将所述元素放在页面上。
You can't do anything with the hidden element until after the $.ajax call is finished, so you should have your code dealing with that element in a function that is called in the success
function of the $.ajax
call. 在$ .ajax调用完成之前,您不能对隐藏元素进行任何操作,因此您应该让代码在$.ajax
调用success
函数中调用的函数中处理该元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.