2016-06-27 82 views
0

我有一個圖像矩陣。每張圖片當前都會填充信息的特定字段。代碼爲下面的一個圖像。有沒有辦法使用onclick方法填充多個字段

<a href="#riskrating1" data-toggle="tab"><img src="components/com_safety101/images/a2.jpg" width="72" height="32" border="0" onclick="document.getElementById('jform_pre_control_risk').value = '10: Undesirable'; " /></a>

有沒有一種方式,每個圖像可以填補與不同的信息三個字段?不確定是否可以堆疊document.get ElementById調用?

+0

我認爲必須有一個模式填充輸入元素.. – Rayon

回答

1

你將代碼解壓到一個JavaScript函數:

<a href="#riskrating1" data-toggle="tab"> 
    <img 
     src="components/com_safety101/images/a2.jpg" 
     width="72" height="32" border="0" 
     onclick="populateFields()" /> 
</a> 

<script type="text/javascript"> 
    function populateFields() { 
     document.getElementById('jform_pre_control_risk').value = '10: Undesirable'; 
     // Populate other fields here 
     document.getElementById('other_id').value = 'some other value'; 
    } 
</script> 
2

本聲明onclick屬性可以包含任意數量的JavaScript語句的。
如果你願意,你可以寫一個完整的程序 - 只是看起來很糟糕。

更明智的把你想用onclick實現一個單獨的函數裏面是什麼:

<a href="#riskrating1" data-toggle="tab"> 
    <img src="components/com_safety101/images/a2.jpg" 
     width="72" height="32" border="0" 
     onclick="handleOnClickFor(this)" /> 
</a> 

,並添加JavaScript函數:

<script type="text/javascript"> 
    function handleOnClickFor (element){ 
     // 'element' would be the DOM Object for the <img> tag, 
     // differentiate different images with arguments like this 
     // Note that the arguments can be a string, number, object ... 
     document.getElementById('jform_pre_control_risk').value = '10: Undesirable'; 
} 
</script> 
相關問題