简体   繁体   English

通过调用javascript函数在html中设置参数的值

[英]Setting value of a parameter in html by calling javascript function

Is there a way to do this? 有没有办法做到这一点? <'param name = "ticket" value = "getTicket()"> <'param name =“门票” value =“ getTicket()”>

getTicket() is my Javascript function. getTicket()是我的Javascript函数。 Whatever the function returns, that should be the value of the parameter tag. 无论函数返回什么,都应该是参数标记的值。

I cant set it explicitly using Javascript "document.getElem...." because I want the value to be loaded at the time of page load. 我无法使用Javascript "document.getElem...."对其进行显式设置,因为我希望在页面加载时加载该值。 Or, while the parameter is being set. 或者,在设置参数时。

For further info, I am trying to do this for Tableau Trusted Authentication . 有关更多信息,我正在尝试进行Tableau可信身份验证

Are you using server-side scripting? 您正在使用服务器端脚本吗?

Isn't more effectively to do what you do using the request/response ways? 使用请求/响应方式做事不是更有效吗?

Like directly using <%=ticketValue%> (ASP way) or ${ticketValue} (JSP way)? 像直接使用<%=ticketValue%> (ASP方式)还是${ticketValue} (JSP方式)一样?

You may use Document.Write as follows: 您可以按以下方式使用Document.Write:

<'param name = "ticket" value = "<script>
document.write(getTicket());
</script>">

You can access node from JavaScript right after it was created. 创建节点后,您可以立即从JavaScript访问节点。 You're not obligated to wait for DOMContentLoaded event. 您没有义务等待DOMContentLoaded事件。 So this code will work as expected: 因此,此代码将按预期工作:

<script>
  function getTicket() {
    return 'whatever';
  }
</script>

<param name="ticket" value="" class="js-ticket">
<script>
  var ticket = document.querySelector('.js-ticket');
  ticket.value = getTicket();

  console.log(ticket);
</script>

See demo . 参见演示

This approach is better than using document.write , see good explanation . 这种方法比使用document.write更好,请参见很好的解释

You can use jquery $(document).ready() function callback. 您可以使用jquery $ {document).ready()函数回调。 If jquery isn't available, then you can use the equivalence of it. 如果jquery不可用,则可以使用它的等效项。

$(document).ready equivalent without jQuery $(document).ready等同于没有jQuery

Then you can access document.getElement. 然后,您可以访问document.getElement。

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

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