2012-01-12 111 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