我正在内部网页上工作,以帮助自动执行一些电子邮件任务。我目前有1个表单,该表单具有一些通用信息,这些信息在以下所有表单中都相同,而其他几种表单则具有一些自定义信息。我的问题是我无法从信息表单中获取数据以复制到页面上的其他表单。我的代码如下:

<html>
</body>
<script language="JavaScript">
function updateInput(ish){
    document.getElementById("emailsubj").value = ish;
}
</script>
<body>

Info Form
      <form id="info" name="info">
      Email Subjects:<input name="emailsubj" value="Enter Subject Here">
      </form>
<br><br>

Form 1
      <form id="form1" name="form1">
      Email<input name=emadd value="[email protected]">
      Subject<input name="subj" onblur="updateInput(this.value)">
      </form>
<br><br>

Form 2
      <form id="form2" name="form2">
      Email<input name=emadd value="[email protected]">
      Subject<input name="subj" onblur="updateInput(this.value)">
      </form>
</body>
</html>


所有非信息表单上都将带有一个提交按钮,以执行适当的功能。我不反对使用onchange而不是onblur,但是我的测试没有任何帮助。提前致谢!

最佳答案

jQuery答案
http://codepen.io/anon/pen/aKIgc

的HTML

<html>
    <head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
    </head>
    <body>
        Info Form
        <form id="info" name="info">
            Email Subjects:<input name="emailsubj" placeholder="Enter Subject Here">
        </form>

        Form 1
        <form id="form1" name="form1">
            Email<input name="emadd" placeholder="[email protected]">
            Subject<input name="subj">
        </form>

        Form 2
        <form id="form2" name="form2">
            Email<input name="emadd" placeholder="[email protected]">
            Subject<input name="subj">
        </form>
    </body>
</html>


脚本

$('input[name=emailsubj]').on("keyup",function(e){
  $('[name=subj]').val($(this).val())
});


没有jQuery答案
http://codepen.io/anon/pen/BzKxG

<html><body>

Info Form
<form id="info" name="info">
    Email Subjects:<input name="emailsubj" value="Enter Subject Here" onblur="updateInput(this.value)">
</form>
<br/><br/>

Form 1
<form id="form1" name="form1">
    Email<input name="emadd" value="[email protected]">
    Subject<input name="subj">
</form>
<br/><br/>

Form 2
<form id="form2" name="form2">
    Email<input name="emadd" value="[email protected]">
    Subject<input name="subj">
</form>
</body></html>


JS

function updateInput(ish){
    var a = document.getElementsByName("subj");
    for(var i in a){
        a[i].value = ish;
    }
}

关于javascript - 将数据从一种形式复制到多种形式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15302305/

10-11 19:05