onclick javascript将值发送给另一个控件

时间:2014-04-17 13:07:41

标签: javascript sharepoint user-controls

我想将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>

1 个答案:

答案 0 :(得分:0)

我认为您可以采取一些措施来改进您的代码段。

要开始onclick=""语法不需要javascript:部分,您只在锚标记hrefs中使用javascript:,因此会更改为

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

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

e.g。

<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);
   }
}

看看这是否有帮助。