2012-01-12 107 views
1

我有地址,我已經生成的其他值的列表,我已經包括了各列旁邊的鏈接(見下圖): enter image description here填充隱藏表單輸入的onclick

我需要能夠單擊行旁邊的「計劃此清理」鏈接,然後讓它將該特定信息發送到頁面下方的某些隱藏輸入字段,而無需重新加載頁面。我需要使用一個onClick事件來使用各種數據填充多個輸入。

我已經在使用jQuery,所以也歡迎任何jQuery特定的解決方案。

任何幫助將不勝感激。謝謝!

回答

6

其直線前進,即使沒有JQuery的:

<input ... onclick="return myFunction();"> 

function myFunction(){ 
    document.getElementById("myHiddenInput").value = "some value that i want to have"; 
return true; //if you want to proceed with the submission or whatever your button does, otherwise return false 
} 
+0

如何使用它來使用一次點擊事件填充多個隱藏的輸入,每個輸入都有不同的數據? – Jeremy 2012-01-12 02:24:03

+0

我想通了,並使用以下內容: 'function ElementContent(id,content){ document.getElementById(id).value = content; } 由於某種原因,setValue()不適用於我,所以我只是使用value = – Jeremy 2012-01-12 03:09:40

+0

你是正確的setValue來自不同的API :)也只是一個FYI的JS命名標準調用functionNames是一個以小寫字母開頭的駱駝案例。 – dbrin 2012-01-12 05:03:14

0

假設下面的

ID for Schedule this cleaning = scheduleCleaning 
ID for Regular Cleaning Address = regCleaning 
ID for Vacancy Cleaning Address = vacCleaning 
ID for Deep Cleaning Address = deepCleaning 
ID for hidden will be same as above with _hid 

您可以使用以下

$('#scheduleCleaning').click(function() { 
    $('#regCleaning_hid').val($('#regCleaning').val()); 
    $('#vacCleaning_hid').val($('#vacCleaning').val()); 
    $('#deepCleaning_hid').val($('#deepCleaning').val()); 
} 
1

從我的問題中獲得的信息,您只需查詢document.getElementById("target-input").value="desired value";,或者您是否在尋找其他的東西?

+0

這很完美,謝謝。 – Jeremy 2012-01-12 18:09:02

0

假設 「計劃此清洗」 是

<a class="cleaning" data-cleaning-type="regular">Schedule this cleaning</a> 
<a class="cleaning" data-cleaning-type="vacancy">Schedule this cleaning</a> 
... 

,並在頁面往後你有

<input type="hidden" class="cleaning-schedule current" /> 

<input type="hidden" class="cleaning-schedule regular" /> 
<input type="hidden" class="cleaning-schedule vacancy" /> 
... 

<input type="hidden" class="cleaning-schedule-other regular" /> 
<input type="hidden" class="cleaning-schedule-other vacancy" /> 
... 

然後將下面的代碼將工作

$("a.cleaning").click(function() { 
    var $this = $(this); 

    //the current cleaning type 
    var cleaningType = $this.data("cleaningType"); 

    //set the cleaning schedule 
    $("input.cleaning-schedule.current").val(cleaningType); 

    //set other type specific information 
    $("input.cleaning-schedule." + cleaningType)).val([whatever data you want]); 
    $("input.cleaning-schedule-other." + cleaningType).val([whatever data you want]); 
    return false; // edited 
}); 

我可能會放鏈接中數據屬性中的額外特定數據。


我編輯了這個以反映更廣泛的案例。另請參閱jQuery's data function