onclickjavascript将值发送到另一个控件

onclick javascript send value to another control

本文关键字:另一个 控件 onclickjavascript      更新时间:2023-09-26

我想将EmployeeName中的值发送到sharepoint文本过滤器控件,但当我使用以下代码时,整个页面都会冻结

<div class="ui-widget">
    <label for="EmployeeName">
        Type Employee Name</label>
    <input id="EmployeeName">   
    <input type="button" value="OK" onclick="javascript:moveValue()" id="btnSearch" name="btnSearch" /> 
</div>

<script type="text/javascript">
    function moveValue() {
    var searchTXT = document.getElementById("EmployeeName").value;
    document.getElementById("ctl00_m_g_ae01f1bd_e6a3_4044_8045_ab8b29c41f89_SPTextSlicerValueTextControl").value = SearchTXT;
    }
<script>

我认为您可以做一些事情来改进您的代码片段。

要启动onclick=""语法,不需要javascript:部分,只在锚标记hrefs中使用javascript:,因此将更改为

<input type="button" value="OK" onclick="moveValue()" id="btnSearch" name="btnSearch"/>

其次,你的<input id="EmployeeName">应该有一个指定的类型,可能还有一个值(我不知道你是否为了这个例子而省略了它)

例如

<input id="EmployeeName" type="hidden" value="John Smith" />

最后,函数moveValue容易受到空引用异常的影响。

我会把它改成:

function moveValue() {
   var employeeField = document.getElementById("EmployeeName");
   var otherControl = document.getElementById("ctl00_m_g_ae01f1bd_e6a3_4044_8045_ab8b29c41f89_SPTextSlicerValueTextControl");
   //Making sure both fields are not undefined before accessing their properties.
   if(employeeField && otherControl){
      otherControl.value = employeeField.value;       
   }else{
     //Use for debugging, delete/comment once done
     alert("employeeField: " + employeeField  + ", otherControl: " + otherControl);
   }
}

看看这是否有帮助。