5

我使用 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()

知道我做错了什么吗?

4

4 回答 4

10

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} )
于 2012-06-08T17:01:22.620 回答
2

做这个:

window.onload = function() { 
    document.getElementById('id_TeamField').addEventListener('change', function() {
        alert('test');
    });
};

或者使用 jQuery:

$('#id_TeamField').on('change', function() {
    alert('test');
});
于 2012-06-08T16:44:07.873 回答
0

你可以这样做

添加事件监听器

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>
于 2012-06-08T17:27:10.607 回答
0

这与其他人所说的有点多余,但分享一下,因为它更直接:

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>

于 2018-07-20T23:18:30.520 回答