0

我有一个简单的 div 用于显示用户帐户信息,例如名字:

<div id="firstNameChange"><?=$firstName?></div>

我有用于将其转换为输入元素的 jquery 代码:

//execute when nameChange DIV is clicked
        $("#firstNameChange").click(function(){ 


                //check if there are any existing input elements
                if ($(this).children('input').length == 0){

                    $("#saveFirstName").css("display", "inline");

                    //variable that contains input HTML to replace
                    var inputbox = "<input type='text' class='inputbox' name='firstName' value=\""+$(this).text()+"\">";    
                    //insert the HTML intp the div
                    $(this).html(inputbox);         

                    //automatically give focus to the input box     
                    $("this .inputbox").focus();

                    //maintain the input when it changes back to a div
                    $("this .inputbox").blur(function(){
                        var value = $(this).val();
                        $("#firstNameChange").text(value);

                    });
                }               
        });

我创建了一个 PHP 变量来获取输入元素的内容,但它没有填充变量。出于某种原因,没有使用我在上面创建的 JS 输入元素的名称值设置该变量。任何想法为什么会这样?

if(isset($_POST['submit'])){
$firstName = $_POST['firstName'];
$sql = "UPDATE `user_accounts` SET `first_name`='$firstName' WHERE email='" . $_SESSION['email'] . "' AND user_id=" . $_SESSION['userID'] ;
$_dbConn->update($sql) or die(mysql_error());
Redirect_To('index.php?page=userProfile');  

}

4

2 回答 2

2

问题是您在执行此操作时替换了输入元素:

$("#firstNameChange").text(value);

一种解决方案是将您的 HTML 更改为:

<div id="firstNameChange"><?=$firstName?></div><input type="hidden" name="firstName" id="firstNameHidden"/>

然后在你的模糊函数中将隐藏的输入值设置为文本输入值,如下所示:

$("#firstNameHidden").val( value );
于 2013-01-15T02:54:42.587 回答
1

你为什么做这个?$("这个 .inputbox").focus();

首先,“这个.inputbox”一开始是没有意义的。

 $(inputbox).focus();

 //maintain the input when it changes back to a div
 $(inputbox).blur(function(){
     var value = $(this).val();
     $("#firstNameChange").text(value);
 });

另外,您是否意识到当您执行 $(this).html(inputbox); 时 你用新创建的输入元素替换你的 DIV 的所有内容?所以,你<?=$firstName?>在那一点上已经走了。

于 2013-01-15T02:51:32.790 回答