2014-02-05 35 views
0

我嘗試爲輸入'pid'屬性設置新值並獲取此值,但我得到null如何獲得輸入新的屬性值

我有這樣的代碼jQuery中:

$(".feauter-product-holder").click(function() { 
    $("#productsubject").attr("pid", $(this).find("input[type=hidden]").attr("id")); 
}); 

和我的代碼在HTML:

<div class='feauter-product-holder'> 
    <input type="hidden" id="3"> 
</div> 
<asp:TextBox runat="server" ID="productsubject" pid=""></asp:TextBox></div> 
$.ajax({ 
        url: editor.config.saveSubmitURL,//the url to post at... configured in config.js 
        type: 'POST', 
        data: { servicename: "savedata", text: data, subject: $("#productsubject").val(), pid: $("input[id$='productsubject").attr("pid") },//editor.name contains the id of the current editable html tag 
       }) 

我編輯這個職位 我寫錯誤的類股利,但在我的代碼是正確的。 當我打電話給阿賈克斯pid我得到空

我該怎麼辦?

+0

'類='feauter - 產品 - holder'' – rps

+0

我已經更新了我的帖子 – user3243749

回答

1

首先刪除「。」從DIV類名,然後 你可以嘗試這樣的: -

$(".feauter-product-holder").click(function() { 
    $("input[id$='productsubject']").attr("pid", $(this).find("input[type=hidden]").attr("id")); 
}); 

雖然與ASP的控制處理與jQuery你使用像(元素與選擇結束) 例如: - $("input[id$='productsubject']") .. $("#productsubject")將無法​​正常工作。

實際上,在ASP.NET中,如果您使用具有某些ID的asp控件,則ASP.NET Naming container會更改控件ID,如ctl001_yourcontrolID以避免衝突。因此,在Jquery中,當你通過簡單的ID選擇一個元素(如$("#productsubject"))時,你不會得到那個元素,因爲它的ID已經改變了。爲了說明,你可以在Firebug或鉻的檢查元素中檢查asp控件。

因此,當您編寫$("input[id$='productsubject']")時,它會選擇ID號以productsubject結尾的元素。

+0

我已更新我的帖子 – user3243749

+0

查看最新的回覆 – Pranav

+0

謝謝,我用你的代碼,但我得到空 – user3243749

5

由於錯誤的css類調用,無法找到您的DIV。試試:

<div class="feauter-product-holder"> 
+0

我更新了我的帖子 – user3243749

0

您有2個問題 - div上的類不正確,並且.net會更改文本框的ID。

嘗試

<div class="feauter-product-holder"> 

的股利。

如果您使用的是.NET 4,你可以像在文本框中設置的ClientIDMode =「靜」,這

<asp:TextBox runat="server" ID="productsubject" pid="" ClientIDMode="Static"></asp:TextBox></div> 
+0

我已更新我的文章 – user3243749