繁体   English   中英

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

[英]How to send two parameters with Javascript function on a select option

我做了一个Javascript函数

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

我与选择选项一起使用

<form action="proprete.php" method="post">  
<select id="zone" name="zone" onChange="doReload(this.value);"/> 
//code 
</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'>
  <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>

为了便于将字段留空的值,可以这样更改javascript。

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);

我不太确定我是否理解你的要求。 我同意@RamRaider“其他动态变量/参数从何而来?” 一旦有了参数,就可以尝试使用如下parameters对象:

let parameters = {zone:"a", id:"12", else:"test"}
let urlString = "proprete.php?";
for (var key in parameters) {
    if (parameters.hasOwnProperty(key)) {
       urlString += "&"+key+"="+parameters[key];
    }
}

暂无
暂无

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

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