[英]onChange event doesn't trigger
我使用 Internet Explorer 8 和 Sharepoint 2007。
簡報:我有一個 NewForm,其中包含列表中的各種字段。 我需要使用 javascript,從其中一個字段中獲取用戶引入的值(在文本框中),然后使用 onChange 將其粘貼到同一頁面中的其他字段(其他文本框)上。
問題:我是 JavaScript 新手,我無法設置事件 'onChange' 來觸發我的函數,或者至少觸發一個 'alert("Test") ... 或者只是我做錯了不知道為什么(更有可能)...
讓我們假設我想要使用的字段具有 FieldName="IDTeam"、type="text" 和 id="id_TeamField"。
//My JS below "PlaceHolderMain"
_spBodyOnLoadFunctionNames.push("setTxtBoxesValues");
function setTxtBoxesValues()
{
//snipped
getField('text','IDTeam').onChange = function(){alert('Test')};
//"getField('text', 'IDTeam).onChange = function(){myFunction()};"
//If I want to call myFunction() when onChange event is triggered
}
另外,而不是getField,嘗試了這個:
document.getElementById('id_TeamField').onChange = function(){alert('Test')};
如果我想在觸發 onChange 事件時調用 myFunction()
知道我做錯了什么嗎?
onchange 是一個事件,所以你要么把它放在這樣的標簽中:
<input type="text" id="IDTeam" onchange="(call function here)" />
或者您將 addEventListener 應用於該 DOM 對象:
document.getElementById("IDTeam").addEventListener("change", function() {//call function here});
在 IE6 中,您可能需要:(不確定它是否有效,因為現在很少有人使用 ie6)
document.getElementById("IDTeam").attachEvent("onchange", function() {//call function here} )
做這個:
window.onload = function() {
document.getElementById('id_TeamField').addEventListener('change', function() {
alert('test');
});
};
或者使用 jQuery:
$('#id_TeamField').on('change', function() {
alert('test');
});
你可以這樣做
添加事件監聽器
document.getElementById('id_TeamField').addEventListener('change', function(){alert('test');});
要么
將 onChange 添加到標簽
<select onChange='function() { alert('test');}' name='IDTeam' id='id_TeamField' >
<option>option1</option>
<option>option2</option>
<option>option3</option>
</select>
這對於其他人所說的有點多余,但分享一下,因為它更直接:
const select = document.getElementById('select') const newText = document.getElementById('newText') select.addEventListener("change", e => { console.log(e.target.value) newText.value = e.target.value })
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <select id="select"> <option value="url1">tool_ver1</option> <option value="url2">tool_ver2</option> <option value="url2" selected >tool_ver3</option> </select> <button type="submit">Retrieve New Release App Options</button> <div class="textInput spacer"> <h2>New Release App Options</h2> <textarea id="newText"></textarea> </div> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.