2012-01-27 121 views
0

我有一個Java腳本代碼將在一個形式JavaScript表單屬性無效錯誤

function editCategory(categoryId) { 
$.ajax({ 
    type: "POST", 
    url: "/product/fetchEditCategory", 
    data: "categoryId=" + categoryId, 
    success: function(response){ 
     var productManagerForm = document.getElementById('productManager'); 
     productManagerForm.ceName.value = response.catName;........ 

我的JSP是設置一些值 -

<form id="productManager" name="productManager" action="/product/" method="post"> 
    <div id="editCategory"> 
     <tr> 
       <td style="font-weight:bold;">Category Name</td> 
       <td><input type="text" id="ceName" /></td>    
      </tr> 
</div>  
<td><a href="#editCategory" id="cat" onclick="editCategory('p1')">edit</a></td> 

我正在運行這段代碼下面的錯誤 -

Message: 'ceName' is null or not an object 

有人可以告訴我這裏有什麼問題嗎?

回答

2

ceName無法作爲productManagerForm的屬性進行訪問。

試試這個:

var ceName = document.getElementById('ceName'); 
ceName.value = response.catName;........ 
+0

This Works ....感謝您的幫助 – Sachin 2012-01-27 18:16:35

0

你一定要試試這個:

var productManagerForm = document.getElementById('productManager'); 
var ceName = productManagerForm.getElementById('ceName'); 
ceName.value = response.catName; 
+0

或GZ說什麼 – 2012-01-27 18:05:56

0

不知道你想什麼用此功能acheive,但

var productManagerForm = document.getElementById('productManager'); 

返回節點元件。 ceName是這個元素的一個子元素。不是這個元素的屬性。您可以通過使用直接獲得元素:

var ceName = document.getElementById("ceName"); 

或下降的節點樹,直到找到這個......考慮ceName是ID,它是更快得到它直接使用到的getElementById() 。

希望這會有所幫助!

相關問題