简体   繁体   English

需要帮助使JS函数更加动态

[英]Need help making JS functions more dynamic

What I want to do is use the JS functions for additional input fields in the form. 我想要做的是将JS函数用于表单中的其他输入字段。 For instance, I want to have a LastName and LastNameLabel, etc. I am hoping I can adjust the functions in some way to find pairs Aaa and AaaLabel so that every field can have the same effect. 例如,我想要一个LastName和LastNameLabel等。我希望可以通过某种方式调整函数以找到Aaa和AaaLabel对,以便每个字段都可以具有相同的效果。 My code is below. 我的代码如下。 Thanks! 谢谢!

Update: I am now just trying to get the 'Original Value' to show the name of the element ID with a space between a lowercase letter followed by an uppercase letter. 更新:我现在正试图获取“原始值”以显示元素ID的名称,在小写字母后跟大写字母之间留一个空格。 For example, if the element ID is LastName, I want the innerHTML to be "Last Name". 例如,如果元素ID为LastName,则我希望innerHTML为“ Last Name”。 Thanks for all your help!! 感谢你的帮助!!

<script type="text/javascript">
  function LabelOnClick(el) { var associatedFieldId = el.getAttribute('for');
                              if (associatedFieldId)
                                 document.getElementById(associatedFieldId).focus();
                            }
  function FieldOnFocus(el) { document.getElementById(el.id + 'Label').className='FieldFocus';
                            }
  function FieldOnKeyPress(el) { if (event.keyCode!=9 && event.keyCode!=8) { document.getElementById(el.id + 'Label').innerHTML='';
                                                                           }
                               }
  function FieldOnKeyDown(el) { el.style.width = Math.max(30,el.value.length*10)+'px';
                            }

  function FieldOnKeyUp(el) { el.value=el.value.substring(0,1).toUpperCase()+el.value.substring(1,el.value.length);
                              if (el.value=='') document.getElementById(el.id + 'Label').innerHTML='Original Value';

                            }
  function FieldOnBlur(el) { if (el.value=='') { document.getElementById(el.id + 'Label').className='FieldBlur';
                                                 document.getElementById(el.id + 'Label').innerHTML='Original Value';
                                               }
                           }
</script>

<style>
  .InputField { border: 1px dotted lightgray;
                border-bottom:1px solid black;
                min-width: 30px;
                padding: 5px;
                font-family:tahoma, arial, sans-serif;
                font-size: 14px;
  }
  .FieldBlur { color: gray;
               position: absolute;
               padding: 6px;
               padding-left: 8px;
               padding-top: 8px;
               font-family: tahoma, arial, sans-serif;
               font-size: 14px;
  }
  .FieldFocus { color: lightgray;
                position: absolute;
                padding: 6px;
                padding-left: 8px;
                padding-top: 8px;
                font-family: tahoma, arial, sans-serif;
                font-size: 14px;
  }
  .FieldInput { color: black;
                position: absolute;
                padding: 6px;
                padding-left: 8px;
                padding-top: 8px;
                font-family: tahoma, arial, sans-serif;
                font-size: 14px;
  }
</style>

<label id="FirstNameLabel" for="FirstName" class="FieldBlur"
       onclick="LabelOnClick(this)">First Name
</label>

<input id="FirstName" class="InputField" type="text" name="FirstName" maxlength="25"
       onfocus="FieldOnFocus(this)"
       onkeypress="FieldOnKeyPress(this)"
       onkeydown="FieldOnKeyDown(this)"
       onkeyup="FieldOnKeyUp(this)"
       onblur="FieldOnBlur(this)"/>

</body>

OK, ignoring the fact that inline event handlers are generally frowned upon these days, try something like the following, in which the onclick and onfocus in your HTML pass this through to the associated functions such that those functions have a reference to the element that the event was triggered on so that they can figure out what the related field or label is: OK,忽略了一个事实,即内嵌事件处理程序一般是在这些天皱起了眉头,你可以试试以下,其中onclickonfocus在你的HTML通过this通过这种关联的功能,这些功能必须的元素的引用触发了事件,以便他们可以找出相关的字段或标签是什么:

<script>

function LabelOnClick(el) {
   var associatedFieldId = el.getAttribute("for");
   if (associatedFieldId)
      document.getElementById(associatedFieldId).focus();
}

function FieldOnFocus(el) {
   document.getElementById(el.id + "Label").className='FieldFocus';
}

</script>

<label id="FirstNameLabel" for="FirstName" class="FieldBlur"
       onclick="LabelOnClick(this)">First Name  </label>

<input id="FirstName" ... onfocus="FieldOnFocus(this)" ... />

I'll leave doing something similar for the other events as an exercise for the reader... 我将在其他事件中做类似的事情作为读者的练习...

EDIT to match your updated question: 编辑以匹配您更新的问题:

If this: 如果这:

document.getElementById(el.id + 'Label').innerHTML='Original Value'; 

is the part you're talking about where you want to replace 'Original Value' with the elements ID with spaces added then at that point el.id holds "LastName" so if you use a regex like in the following line you should get "Last Name": 是您要讨论的部分,您要在其中用添加的空格替换元素ID的'Original Value' ,然后在这一点上el.id保留“ LastName”,因此,如果您使用正则表达式(如下面的行),则应获得“姓”:

document.getElementById(el.id + 'Label').innerHTML
                      = el.id.replace(/([^A-Z])([A-Z])/g, "$1 $2");

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

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