2013-03-11 43 views
2

我有一个PHP变量,我试图在不使用'value'属性的情况下出现在输入字段上,但似乎无法使其工作。在不使用value属性的情况下在输入字段上显示var

我尝试了一个jQuery代码,我发现第一个'追加'变量到输入字段,但没有奏效。

var first_name = "<?php echo $current_user['first_name']; ?>"; 
    alert(first_name); 
    $('#first_name').val($('#first_name').val() + first_name); 

然后我尝试使用getElementById,但它也没有工作。

var first_name = "<?php echo $current_user['first_name']; ?>"; 
    alert(first_name); 
    document.getElementById('first_name').value = first_name; 

任何建议将不胜感激!

编辑:

这是HTML表单

<form name="personal_form" action="includes/editpersonal.php" method="POST"> 
    <fieldset> 
     <legend><h2>Personal Details</h2></legend> 
     <table class="personalform"> 
     <!--if there is an error on first name, the class name 'form_error_row' will be input into the following tr--> 
     <!--allows me to colourise the text field to indicate an error on this row--> 
     <tr class="<?php echo form_row_class("first_name") ?>" > 
      <th><label for="first_name"><p>First Name: </p></label></th> 
      <td> 
      <input type="text" name="first_name" id="first_name" size="40" /> 
      <!--if there is an error it will print out a div with the error message--> 
      <?php echo error_for('first_name') ?> 
      </td> 
      </tr> 
     </table> 
    </fieldset> 
    <input type="submit" class="reg-button" value="Update Records" /> 
</form> 
+2

http://api.jquery.com/ready/ – undefined 2013-03-11 18:33:03

+0

u能告诉我的代码 – PSR 2013-03-11 18:33:08

+0

@PSR检查编辑请 – 5attab 2013-03-11 18:45:46

回答

1

尝试在document.ready分配值,以确保HTML元素都准备好被脚本访问。

$(document).ready(function(){ 
$('#first_name').val($('#first_name').val() + first_name); 
}); 
+0

这没有奏效,还有什么建议? @adil – 5attab 2013-03-11 18:44:30

相关问题