无法获得第二个javascript函数吗?

时间:2012-12-17 09:40:40

标签: javascript jquery html function url

我无法获得第二个javascript函数。 当我点击“发送邮件”按钮时,它应该调用第二个函数并将这两个值传递给它。 href行(第一个函数中的第二个最后一行)未正确呈现。

<script>
function getvals(first,second) {
    alert(''+first+'');
    alert(''+second+'');
    mywindow=window.open('','Send Mail','height=200,width=400');
    mywindow.document.write("<FORM NAME='test'>");
    mywindow.document.write("<table align='center'><tr><td>User/Group: </td><td><input type='text' id='newfirst' name='iuser'></td></tr>");
    mywindow.document.test.iuser.value = ''+first+'';
    mywindow.document.write("<tr><td>Issue Key: </td><td><input type='text' id='newsecond' name='ikey'></td></tr>");
    mywindow.document.test.ikey.value = ''+second+'';
    mywindow.document.write("<tr><td><a href='javascript:popitup(document.getElementById('newuser').value,document.getElementById('newkey').value);'>Send Mail</a></td></tr></table>");
    mywindow.document.write("</FORM>");
}
function popitup(user,key) {
    alert(''+user+'');
    alert(''+key+'');
    var url = 'http:\/\/localhost:8080/plugins/servlet/mailservlet?receiver=' + user + '&issue=' + key;
    newwindow=window.open(url,'name','height=400,width=400');
    if (window.focus) {newwindow.focus()}
}
</script>

4 个答案:

答案 0 :(得分:3)

函数popitup将不会被调用,因为它是在父窗口中写入的,而不是在window.open打开的窗口中。在函数调用中尝试window.opener

答案 1 :(得分:1)

嵌套引号问题,你可以正确地逃避它:

 mywindow.document.write("<tr><td><a href='javascript:popitup(document.getElementById(\"newuser\").value,document.getElementById(\"newkey\").value);'>Send Mail</a></td></tr></table>");

答案 2 :(得分:0)

See it working

  function getvals(first,second) {
      alert(''+first+'');
      alert(''+second+'');
      mywindow=window.open('','Send Mail','height=200,width=400');
      mywindow.document.write("<FORM NAME='test'>");
      mywindow.document.write("<table align='center'><tr><td>User/Group: </td><td><input type='text' id='newfirst' name='iuser'></td></tr>");
      mywindow.document.test.iuser.value = ''+first+'';
      mywindow.document.write("<tr><td>Issue Key: </td><td><input type='text' id='newsecond' name='ikey'></td></tr>");
      mywindow.document.test.ikey.value = ''+second+'';
      mywindow.document.write("<tr><td><a href=\"javascript:opener.popitup(document.getElementById('newfirst').value,document.getElementById('newsecond').value);\">Send Mail</a></td></tr></table>");
      mywindow.document.write("</FORM>");
  }

  function popitup(user,key) {
      alert(''+user+'');
      alert(''+key+'');
      var url = 'http:\/\/localhost:8080/plugins/servlet/mailservlet?receiver=' + user + '&issue=' + key;
      newwindow=window.open(url,'name','height=400,width=400');
      if (window.focus) {newwindow.focus()}
  }

问题已解决:
1)window.open中的转义问题 2)函数调用应该是opener.popitup
3)用户和密钥的Id名称不正确。

答案 3 :(得分:0)

请不要这样做(文件写)

使用appendChild,并在那里附加onclick-handler