简体   繁体   English

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

[英]onclick javascript send value to another control

I would like to send value from EmployeeName to a sharepoint text filter control but the entire page freezes up when I use the following code 我想将值从EmployeeName发送到共享点文本过滤器控件,但是当我使用以下代码时,整个页面冻结

<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>

I think there are a couple of things that you can do to improve your snippet. 我认为您可以采取一些措施来改善代码段。

To start the onclick="" syntax does not need javascript: part, you use javascript: only inside anchor tag hrefs, so that would change to 要启动onclick=""语法,不需要javascript:部分,您只能在锚标记hrefs中使用javascript: :,这样就可以更改为

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

Secondly your <input id="EmployeeName"> should have a type specified and probably a value (I don't know if you omitted it for the purpose of the example) 其次,您的<input id="EmployeeName">应该指定一个类型,并且可能是一个值(我不知道您是否出于示例目的而省略了它)

eg 例如

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

Lastly your function moveValue is susceptible to null reference exceptions. 最后,您的函数moveValue容易受到空引用异常的影响。

I would change it to: 我将其更改为:

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

See if this helps. 看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM