javascript - 使用 framework7 选择单选按钮后重定向到另一个页面

标签 javascript html-framework-7

我无法在选择单选按钮后根据用户选择重定向到页面 X 到页面 Y。控制台给我写信:

Uncaught ReferenceError:  onClickGo is not defined

我在 my-app.json 中声明了页面

routes: [
{
    name: 'home',
    path: '/home/',
    url: './pages/home/home.html'
},
{
    name: 'travel',
    path: '/travel/',
    url: './pages/travel/travel.html',
    routes: [

        {
            name: 'travel-ny',
            path: ' ny/',
            url: './pages/travel/ny.html',
        },
        {
            name: 'travel-la',
            path: ' la/',
            url: './pages/travel/la.html',
        },

        {
            name: 'travel-milan',
            path: ' milan/',
            url: './pages/travel/milan.html',
        },

    ]
}

用户的选择在travel.html中进行

<div data-name="travel" class="page">

<div class="page-content pg-no-padding">

<div class="row">
    <div class="col-100 tablet-100 desktop-100">            
    <form class="list" id="travel-form">
            <ul>
                <li>
                    <label class="item-radio item-content">
                        <input type="radio" name="travel" value="ny" checked="checked"/>
                        <i class="icon icon-radio"></i>
                        <div class="item-inner">
                            <div class="item-title">New York</div>
                        </div>
                    </label>                            
                </li>

                <li>
                    <label class="item-radio item-content">
                        <input type="radio" name="travel" value="la"/>
                        <i class="icon icon-radio"></i>
                        <div class="item-inner">
                            <div class="item-title">Los Angeles</div>
                        </div>
                    </label>                            
                </li>

                <li>
                    <label class="item-radio item-content">
                        <input type="radio" name="travel" value="milan" />
                        <i class="icon icon-radio"></i>
                        <div class="item-inner">
                            <div class="item-title">Milan</div>
                        </div>
                    </label>                            
                </li>

        </form> <!--list--> 

        <div class="block">
            <a class="button button-fill travelChoice" onclick="onClickGo()">Go</a>
        </div>      
    </div><!--col-100-->
</div><!--row-->            

我将 onClickGo 函数放在 travel.js 中,我以这种方式将其包含在 home.html 中

<script type="text/javascript" src="js/travel.js"></script>

travel.js

const onClickGo = () => {
   $$('.travelChoice').on('click', function(){

    //retrieve the value selected
    var selectedValue = $$('.list input:checked').val();
   //go to the page 
   router.navigate('/travel/'+ selectedValue +'/');

}

提前致谢

最佳答案

我认为你需要尝试这个,因为在找到你的函数之前加载 html:

 $$('.travelChoice').on('click', function(){

    //retrieve the value selected
    var selectedValue = $$('.list input:checked').val();
   //go to the page 
   router.navigate('/travel/'+ selectedValue +'/');
}

没有用 onClickGo 变量包装它,也没有从按钮上删除点击事件

<a class="button button-fill travelChoice">Go</a>

...我认为它会起作用......也尝试使用它来导航:

self.app.router.navigate('/travel/'+ selectedValue, {reloadCurrent: true}); 

通过添加 reloadCurrent,您可以从已兑现的 html 页面打开页面而无需公平

关于javascript - 使用 framework7 选择单选按钮后重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082904/

相关文章:

javascript - 清除Jquery UI对话框输入框

javascript - 在小屏幕上固定定位的 html div 有什么好的解决方案?

ios - cordova IOS 应用程序所有 onclick 和后退按钮不适用于最新的 IOS 11.3.1

android - 我如何禁止在特定的 div 上滑动手势?

javascript - 如何从 HTML 文件中获取脚本并将其移动到它自己的 .JS 文件中?

javascript - 无法分配给函数“class Template7”的只读属性 'prototype'

javascript - gzip 一个缩小的 .js 文件

javascript - 当父组件发生变化时更新子组件

javascript - 在 webdriver 中执行的性能影响

jquery - 无法通过ajax调用获取framework7中的php解析数据