简体   繁体   English

渲染从jQuery Ajax中选择Tag,然后从选定的选项中获取值

[英]Render select Tag from jQuery Ajax , then get the value from selected option

Problem : 问题

I create tag element ( <select> ) from Ajax jQuery, but sometimes i can't get the value from its, and sometimes i can get it. 我从Ajax jQuery创建标签元素( <select> ),但有时我无法从其获取值,有时我可以获取它。

Picture 1 *before Ajax : 图片 1 *在Ajax之前: 在此处输入图片说明

Picture 2 *after Ajax : 图片 2 *在Ajax之后: 在此处输入图片说明

Assumptions the elements id is : proyek. 假设元素id是:proyek。

When i tried to console it : $('#proyek').val() , it says undefined , but after i reload and try again the proccess it says it has value 17 (right value). 当我尝试进行控制台操作时: $('#proyek').val() ,它显示为undefined ,但是在我重新加载$('#proyek').val()试该过程后,它说它的值为17 (正确的值)。

So sometimes i can get the value, and sometimes i cant. 所以有时我可以得到价值,有时我不能。


Whole code : 整个代码

html : <span id="generateProyek"></span> html: <span id="generateProyek"></span>

jQuery : $("#generateProyek") .load("{{ this.url.getBaseUri()~"companyprojectdetail/ajaxproyek/" }}"+id,function(){}); jQuery: $("#generateProyek") .load("{{ this.url.getBaseUri()~"companyprojectdetail/ajaxproyek/" }}"+id,function(){});

*note : i use volt, it use {{ }} to echo something. *注意:我使用伏特,它使用{{ }}来回声。

jQuery can't directly address an element that has been created after the page has been loaded. jQuery无法直接处理页面加载后创建的元素。 To get this done, use delegation. 为此,请使用委托。 Since I don't have a bigger picture of your code I'll demonstrate a situation: 由于我对您的代码没有更多了解,因此我将演示一种情况:

We have a div: $('#to_be_loaded_into') Now when we click on $('#proyek') (which is created with ajax request) we want to get the value: 我们有一个div: $('#to_be_loaded_into')现在,当我们单击$('#proyek') (由ajax请求创建)时,我们想要获取值:

$('#to_be_loaded_into').on('click, '#proyek', function(){
console.log($('#proyek').val();
)};

edit: trying to get more specific for your elements, it just might be: $('#generateProyek').on('click, '#proyek', function(){ console.log($('#proyek').val(); )}; 编辑:尝试针对您的元素更具体,可能只是: $('#generateProyek').on('click, '#proyek', function(){ console.log($('#proyek').val(); )};

I must first address an element that has been loaded into the html, and than refer to element inside it that has been renderd dynamically 我必须首先处理已加载到html中的元素,然后引用其中已动态呈现的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM