js将子页面的值返回给父页面

该博客介绍了如何使用JavaScript在子页面(b.html)中获取用户输入并将其值返回到父页面。通过`showModalDialog`函数打开子页面,并在子页面中定义函数`a()`,当用户点击‘传值’按钮时,将输入值与固定字符串拼接,然后通过`parent.window.returnValue`将结果传回父页面。在父页面中,使用`onc()`函数接收返回值并更新表单字段。
摘要由CSDN通过智能技术生成
<html>
<head>
<title>主页面(父页面)</title>
<script language="javascript" type="text/javascript">
function onc(){
	var someValue=window.showModalDialog("b.html","","dialogWidth=500px;dialogHeight=500px;status=no;help=no;scrollbars=no");
	var arr=someValue.split("/");
	document.form1.p1t.value=arr[0];
	document.form1.p2t.value=arr[1];
}
</script>
</head>
<body>
<form name="form1" action="#">
<input type="text" name="p1t">
<input type="text" name="p2t">
<input type="button" value="打开对话框" οnclick="onc()">
</form>
</body>
</html>

<html>
<head>
<title>子页面</title>
<script language="javascript" type="text/javascript"&g
在uniApp中使用Vue3进行父组件通信时,如果页面需要将数据返回给父页面,通常会通过事件总线(Event Bus)或者Vuex状态管理库来进行。这里简单解释一下这两种常见的做法: **1. 使用Event Bus(事件中心):** 页面(例如`child.vue`)可以触发一个自定义事件,然后在父页面(`parent.vue`)监听这个事件并接收数据。 ```javascript // child.vue export default { methods: { sendDataBack() { this.$emit('dataFromChild', { keyData: 'value' }); } } } // parent.vue <template> <view> <child @dataFromChild="handleData"></child> </view> </template> <script> import Child from '@/components/Child.vue'; export default { components: { Child }, methods: { handleData(data) { console.log('Received data:', data); } } }; </script> ``` **2. 使用Vuex(状态管理库):** 如果涉及到更复杂的数据共享和管理,可以将数据存储在Vuex store中,页面更新数据后通过commit动作通知store,然后父页面通过actions获取最新状态。 首先,在`store.js`中创建一个action和mutation: ```javascript const state = {}; const mutations = { setData(state, data) { state.data = data; } }; const actions = { async sendDataToParent({ commit }, data) { await commit('setData', data); } }; export default { state, mutations, actions }; ``` 然后在页面中调用`dispatch`: ```javascript this.$store.dispatch('sendDataToParent', { keyData: 'value' }); ``` 在父页面的组件内通过`mapActions`获取`getData`方法并调用它来获取数据: ```javascript <template> <button @click="getData">Get Data</button> </template> <script> import { mapActions } from 'vuex'; export default { methods: { getData() { this.$store.dispatch('getData'); } }, computed: { // 如果你想直接在模板中显示数据,可以在这里计算 ...mapActions(['getData']) } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值