2017-01-01 28 views
2

我想在單擊鏈接時爲隱藏字段(sub_catid)設置值。我的源代碼如下。如何使用javascript單擊鏈接時設置隱藏字段的值

隱藏的輸入字段

<input type="hidden" name="sub_catid" value="" id="sub_catid"> 

鏈接

<a href="#" data-subcat="3" class="sub-cat-id">Sub Category Name</a> 
+0

您可以包括你有什麼問題在嘗試? – guest271314

+0

您的問題包含答案。你想設置一些鏈接值_onclick_,然後有一個onclick函數爲該鏈接設置隱藏字段的值 –

回答

0

function func(btnId){ 
 
    document.getElementById('sub_catid').value = document.getElementById(btnId).getAttribute('data-subcat'); 
 
    //testing new value 
 
    alert(document.getElementById('sub_catid').value); 
 
}
<input type="hidden" name="sub_catid" value="" id="sub_catid"> 
 
a link 
 
<a id="button" href="#" data-subcat="3" class="sub-cat-id" onclick="func(this.id)">Sub Category Name</a>

+0

我想將該data-subcat =「3」值傳遞給隱藏字段。我怎樣才能做到這一點? –

+0

沒問題..看我編輯的代碼 – Tal87

+0

非常感謝你的大力支持。現在它工作正常。 –

相關問題