2017-04-19 53 views
0

使用此功能,网站将从actions/create_random_profile.php加载用户配置文件。无法从动态加载的内容中获取文本框值

$(function() { 
$('body').on('click','#random_profile',function(){ 
//var msgid = $(this).attr("id"); 
//var dataString4 = 'msgid=' + msgid; 
$('#random_profile_area_loader').html('<img src="img/loader.gif" class="loading" />'); 
$.ajax({ 
    type: "POST", 
    url: "actions/create_random_profile.php", 
    //data: dataString4, 
    cache: false, 
    success: function(data){ 
    if (data == 0) { 
    $('#random_profile_area').html('Not Sent!'); 
    } else { 
    $('#random_profile_area_loader').html(''); 
    $('#random_profile_area').append(data); 
    } 
    } 

}); 
return false; 
}); 
}); 

在这里一切都很好。这是从我的输出create_random_profile.php

</php 
Echo ' 
<div class="col-md-2 created_profile"> 
/////......./////// 
    <div class="form-group"> 
    <label for="email">Email:</label> 
    <input type="text" class="form-control" id="email" name="email" value="'.$email.'"> 
    </div> 
/////......./////// 
Echo '<button type="button" class="btn btn-default save_random_profile" id="'.$rand_identifier.'">Create this profile</button>'; 
</div>'; 
?> 

未来我知道,我必须有唯一的ID,因为多个脚本可以触发一个例子。我知道该怎么做。问题在于: 所有变量都返回未定义的值。 我试图

$("input#bmonth").val(); 

OR

document.getElementById('bmonth').value; 

$("#bmonth").val(); 

问题是whorse当我添加唯一标识符对于每个表格。比方说,变种unique_identifier = 22 我应该使用

$("#bmonth"+unique_identifier).val(); 

<input type="text" id="bmonth22" value="3"> 

获得的价值,但它也不起作用。

我提到它可以在我制作的其他脚本中使用,但我在这里看不到错误。我认为这是因为内容是dinamically加载的,但我使用$('body')。on(),它应该没问题。有任何想法吗?

$(function() { 
$('body').on('click','.save_random_profile',function(){ 
var identifier_id = $(this).attr("id"); 
$('.created_profile').html('<img src="img/loader.gif" class="loading" />'); 
// var phcat = document.getElementById('phcat'+identifier_id).value; 
//var phcat = $('#created_profile'+identifier_id).children().document.getElementById('phcat').value; 
var phcat = "man.png"; 
    var phprofile = "man.png"; 
/*var phcat = document.getElementById('phcat').value; 
var phprofile = document.getElementById('phprofile').value;*/ 

var byear = $("#byear").val();//document.getElementById('byear').value; 
var bmonth = $("input#bmonth").val();//document.getElementById('bmonth').value; 
var bday = $("input#bday").val();//document.getElementById('bday').value; 
var country = $("input#country").val();//document.getElementById('country').value; 
var email = $("input#email").val();//document.getElementById('email').value; 
var password = $("input#password").val();//document.getElementById('password').value; 
var username = $("input#username").val();//document.getElementById('username').value; 
var interested_in = $("#interested_in").val();//document.getElementById('interested_in').value; 
var sex = $("#sex").val();//document.getElementById('sex').value; 
var status = $("#status").val();//document.getElementById('status').value; 
var description = $("#description").val();//document.getElementById('description').value; 
var dataString = 'phcat=' + phcat + '&phprofile=' + phprofile + '&interested_in=' + interested_in + '&sex=' + sex + '&status=' + status + '&description=' + description + '&byear=' + byear + '&bmonth=' + bmonth + '&bday=' + bday + '&country=' + country + '&email=' + email + '&password=' + password + '&username=' + username; 
alert(dataString); 
$.ajax({ 
    type: "POST", 
    url: "actions/save_random_profile.php", 
    data: dataString, 
    cache: false, 
    success: function(data){ 
    if (data == 0) { 
    $('.created_profile').html('Not Sent!'); 
    } else { 
    $('.created_profile').html(data); 
    } 
    } 
}); 
return false; 
}); 
}); 
+0

你可以使用'this.serialize'这将删除您解析JS变量的数量手动阿贾克斯 – Akintunde007

回答

0

问题很简单。

你正试图访问不存在的东西,这就是为什么你得到未定义。 只有存在$(“输入#bmonth”),才能访问它。

从您粘贴的代码。我想,这得到DOM可以通过调用以下网址

"actions/create_random_profile.php" 

的解决方案是创建一个随机配置文件后访问$(“#输入bmonth”)加载的随机轮廓之后。

+0

我尝试存取权限它创建配置文件后,才容易序列化的形式。创建配置文件时,它还会创建用于调用该功能的按钮。所以,如果配置文件没有创建,我不能点击按钮。 –