如何使用Javascript从iFrame响应中设置隐藏字段值?(How do I set Hidden field values from an iFrame response with Javas

编程入门 行业动态 更新时间:2024-10-27 11:26:26
如何使用Javascript从iFrame响应中设置隐藏字段值?(How do I set Hidden field values from an iFrame response with Javascript?)

我在根据从iFrame消息收到的值提交表单时尝试填充3个隐藏的HTML文件。 此表单是使用Drupal Form API构建的

在页面加载时,我将3个隐藏字段附加到表单并设置eventListener

setupMonerisHiddenFormElements() { $('<input type="hidden">').attr({ id: 'responseCode', name: 'responseCode', }).appendTo('form'); $('<input type="hidden">').attr({ id: 'dataKey', name: 'dataKey', }).appendTo('form'); $('<input type="hidden">').attr({ id: 'errorMessage', name: 'errorMessage', }).appendTo('form'); } window.onload = function() { is_production = Drupal.settings.moneris_payment_processor.is_production; qa_gateway_url = Drupal.settings.moneris_payment_processor.qa_gateway_url; prod_gateway_url = Drupal.settings.moneris_payment_processor.prod_gateway_url; gateway_url = ((is_production) ? qa_gateway_url : prod_gateway_url); if (window.addEventListener) { window.addEventListener("message", processMonerisResponse, false); } else { if (window.attachEvent) { window.attachEvent("onmessage", processMonerisResponse); } } setupMonerisHiddenFormElements(); }

我可以在表单上看到这些字段。 这不是问题。

提交表单时,此Drupal表单属性调用函数submitMonerisIframe():

$form['#attributes'] = array('OnSubmit' => 'submitMonerisIframe();');

在这里我发布消息,可以看到我的processMonerisResponse()函数被调用(见下文)

function submitMonerisIframe() { var monFrameRef = document.getElementById('monerisFrame').contentWindow; monFrameRef.postMessage('',gateway_url); }

我添加了调试。 当我提交表单时,我可以看到正确设置了隐藏值。

function processMonerisResponse(e) { console.log(e.data); this.respData = eval("(" + e.data + ")"); $('#responseCode').attr('value',respData.responseCode); $('#errorMessage').attr('value',respData.errorMessage); $('#dataKey').attr('value',respData.dataKey); console.log("---------- RESPONSE -----------------"); console.log("responseCode: " + $('#responseCode').attr('value')); console.log("errorMessage: " + $('#errorMessage').attr('value')); console.log("dataKey: " + $('#dataKey').attr('value')); console.log("-------------------------------------"); e.preventDefault(); }

控制台的结果:

{ “responseCode”: “001”, “dataKey”: “OT-QQHTTg4EDGgBa4mkc0KrCGkPJ”, “彬”: “378734”}

----------回复-----------------

responseCode:001

错误信息:

dataKey:ot-QQHTTg4EDGgBa4mkc0KrCGkPJ


但是当我检查$ _POST []数据时,隐藏的字段在那里但是是空的。

我花了好几个小时尝试不同的东西但似乎什么都没有用

提前致谢!

I am trying to populate 3 hidden HTML files upon submitting a form based on values received from an iFrame message. This form is built using the Drupal Form API

On page load, I append 3 hidden fields to the form and setup the eventListener

setupMonerisHiddenFormElements() { $('<input type="hidden">').attr({ id: 'responseCode', name: 'responseCode', }).appendTo('form'); $('<input type="hidden">').attr({ id: 'dataKey', name: 'dataKey', }).appendTo('form'); $('<input type="hidden">').attr({ id: 'errorMessage', name: 'errorMessage', }).appendTo('form'); } window.onload = function() { is_production = Drupal.settings.moneris_payment_processor.is_production; qa_gateway_url = Drupal.settings.moneris_payment_processor.qa_gateway_url; prod_gateway_url = Drupal.settings.moneris_payment_processor.prod_gateway_url; gateway_url = ((is_production) ? qa_gateway_url : prod_gateway_url); if (window.addEventListener) { window.addEventListener("message", processMonerisResponse, false); } else { if (window.attachEvent) { window.attachEvent("onmessage", processMonerisResponse); } } setupMonerisHiddenFormElements(); }

I can see these fields are being added on the form. This is not the problem.

When the form gets submitted the function submitMonerisIframe() is called by this Drupal form attribute:

$form['#attributes'] = array('OnSubmit' => 'submitMonerisIframe();');

Here i am posting the message and can see that my processMonerisResponse() function is being called (see below)

function submitMonerisIframe() { var monFrameRef = document.getElementById('monerisFrame').contentWindow; monFrameRef.postMessage('',gateway_url); }

I have added my debugging. When I submit the form, I can see that the hidden values are being set correctly.

function processMonerisResponse(e) { console.log(e.data); this.respData = eval("(" + e.data + ")"); $('#responseCode').attr('value',respData.responseCode); $('#errorMessage').attr('value',respData.errorMessage); $('#dataKey').attr('value',respData.dataKey); console.log("---------- RESPONSE -----------------"); console.log("responseCode: " + $('#responseCode').attr('value')); console.log("errorMessage: " + $('#errorMessage').attr('value')); console.log("dataKey: " + $('#dataKey').attr('value')); console.log("-------------------------------------"); e.preventDefault(); }

Result from the console:

{"responseCode":"001","dataKey":"ot-QQHTTg4EDGgBa4mkc0KrCGkPJ","bin":"378734"}

---------- RESPONSE -----------------

responseCode: 001

errorMessage:

dataKey: ot-QQHTTg4EDGgBa4mkc0KrCGkPJ


But when I inspect the $_POST[] data the hidden fields are there but are empty.

I spent hours trying different things but nothing seems to work.

Thanks in advance!

最满意答案

设置属性 value与设置属性 value 。

提交表单时,它是提交的value 属性

使用val()而不是attr()来设置或获取value 。

DEMO

Setting attribute value is not the same as setting the property value.

When you submit the form it is the value property that gets submitted.

Use val() instead of attr() to set or get the value.

DEMO

更多推荐

hidden,form,Drupal,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-07-19 01:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   如何使用   Javascript   iFrame   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!