duanji1610 2018-09-29 11:12
浏览 31
已采纳

如何使用Javascript函数在select选项上发送两个参数

I made a Javascript function

function doReload(zone){  
document.location = 'proprete.php?zone=' + zone;  
} 

that I use with a select option

<form action="proprete.php" method="post">  
<select id="zone" name="zone" onChange="doReload(this.value);"/> 
//code 
</form>

so whenever I change the selected option the page do a reload and send the value in the link.

I'm stuck as I wanted to send another parameter (dynamic) to the link as well but couldn't figure out how..

edit:

the other parameter comes from another form

<form action="proprete.php" method="post">
            <input type="week" name="week" id="week" class="inpBox"  style="height: 30px;width:200px;font-size: 15px;">
            <input type="submit" value="Basculer vers" name="basculer" style="height: 30px;width:150px;font-size: 15px;"/>
        </form> 

Could you please help :)

  • 写回答

2条回答 默认 最新

  • doulun1939 2018-09-29 12:33
    关注

    Late I know but I had a little play around and created a more flexible solution which does not use inline event handlers and will accomodate more forms / elements if required with no further work... it might be of use for future reference maybe.

    <form action='proprete.php' method='post'>
      <input type='number' name='number' id='number' class='inpBox' value=303 style='height: 30px;width:200px;font-size: 15px;'>
    </form>
    
    <form action='proprete.php' method='post'>
      <input type='text' name='username' id='username' class='inpBox' value='sergio' style='height: 30px;width:200px;font-size: 15px;'>
    </form>
    
    
    <form action='proprete.php' method='post'>
      <input type='time' name='time' id='time' class='inpBox' style='height: 30px;width:200px;font-size: 15px;'>
    </form>
    
    <form action='proprete.php' method='post'>
      <input type='week' name='week' id='week' class='inpBox' style='height: 30px;width:200px;font-size: 15px;'>
      <input type='submit' value='Basculer vers' name='basculer' style='height: 30px;width:150px;font-size: 15px;'/>
    </form>
    
    <form action='proprete.php' method='post'> 
      <select id='zone' name='zone'> 
        <option selected hidden='hidden' value=null>Select Zone
        <option>first
        <option>second
        <option>third
        <option>fourth
      </select>
    </form>
    
    <script>
        document.getElementById('zone').addEventListener( 'change', function(e){
            let tmp={};
            /* Find all forms that share the same action (though this query can easily be modifed) and the elements within */
            Array.prototype.slice.call( document.querySelectorAll( 'form[action="proprete.php"] > *:not([type="submit"])' ) ).forEach( function( e ){
                /* add name and value to temporary object */
                tmp[e.name]=e.value;
            });
            /* process the temp object to create a querystring */
            let href=Object.keys( tmp ).map(function( k ){
                return [ k, tmp[ k ] ].join('=');
            }).join('&');
    
            /* optional: change url but stay on same page ~ useful if using ajax perhaps */
            history.replaceState( tmp, null, '?' + href );
    
            alert( href )
    
            /* remove alert & uncomment below to redirect */
            //location.href=href;
        },false);
    </script>
    

    To facilitate a value where a field has been left blank one could alter the javascript like this.

    document.getElementById('zone').addEventListener( 'change', function(e){
        let tmp={};
        /* a default value for empty fields */
        let def='unknown';
    
        /* Find all forms that share the same action (though this query can easily be modifed) and the elements within */
        Array.prototype.slice.call( document.querySelectorAll( 'form[action="proprete.php"] *:not([type="submit"])' ) ).forEach( function( e ){
            /* add name and value to temporary object, using default value for blank fields */
            if( typeof( e.name )!='undefined' ) tmp[e.name]=( e.value=='' ) ? def : e.value;
    
        });
    
        /* ...... etc as before */
    },false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知