将jquery元素转换为html会将输入值更改为输入名称

时间:2014-03-03 21:37:26

标签: jquery html ajax

我很难过。我有一个div,里面有几个元素。其中一些要素是投入。我需要做的是这样用户可以编辑div内的元素。问题是这些输入的值不会显示,而是存储在数据库中。我需要检索这些值并将它们附加到输入,然后将其显示给用户进行编辑。问题是,当我尝试将这些元素转换回html以放入我的编辑器时,一旦附加了新值(其工作正常),如果输入是复选框,则值将替换为名称,如果输入,则值将消失是文字。我有以下代码

var text = $('#parentDiv').html();              //this is the div with several elements inside 
var elements = $(text);                    //convert each of those children into jquery elements
var found = $('input', elements);              //get all inputs
found.each(function(){                         //for each input element
var input = $(this);
var name = input.attr('name');             //get the name of that element
$.post('getinputvalue.php', {id: id, name: name})    //get the saved value of that element from my database
    .done(function(data){
        input.attr('value', data);    //set the value of that input equal to the value retrieved from the database
        $('input[name="'+name+'"]', elements).replaceWith(input);  // replace the original input with the new input element which has the correct value

    });

});
console.log(elements);                          //this shows each element and the inputs have the correct value
var blankDiv = $("<div />").append(elements);  // attach the elements to a div so I can get the new html to input back into a text editor
    console.log(blankDiv.html());                    //the input elements now have the name of the input copied into the value attribute for checkboxes and the value is gone for text inputs instead of the correct value

关于我做错的任何想法?

getinputvalue代码在这里:

<?php session_start();
require_once('../functions/questionfunctions.php');
require_once('../functions/globalfunctions.php');
require_once('../includes/connect.php');

$id = htmlspecialchars($_POST['id']);
$name = htmlspecialchars($_POST['name']);
$cans = getSpecificFormCans($id, $name)->fetchColumn();
echo $cans;

0 个答案:

没有答案