使用一个输入作为两个按钮的值

时间:2016-04-28 00:15:30

标签: javascript html

我想要两个提交相同值的按钮(由输入字段确定)。

<form action="index.html">
    <button type="submit" name="btn1">Btn1</button>
    <button type="submit" name="btn2">Btn2</button>
    <input type="text" value="50">
</form>

应该导致:

Btn1 => index.html?Btn1=50
Btn2 => index.html?Btn2=50

2 个答案:

答案 0 :(得分:1)

根据点击按钮调用index.html。

<script type="text/javascript">
                function setButton1Value(){
//call index.html?Btn1=50                    
}
</script>

<script type="text/javascript">
                function setButton2Value(){
//call index.html?Btn2=50                    
}
</script>

 <form action="index.jsp" method="get">
                <input type="text" value="50" name="input" id="input">
                <button type="submit" name="btn1" id="btn1" onclick="setButton1Value()">Btn1</button>
                <button type="submit" name="btn2" id="btn2" onclick="setButton2Value()">Btn2</button>
 </form>

答案 1 :(得分:0)

两个按钮无法发送相同的值,因为一次只能单击一个按钮。要发送一个值的按钮,您可以尝试: -

    <form action="index.jsp" method="get">
                <input type="text" value="50" name="input" id="input">
                <button type="submit" name="btn1" id="btn1" onclick="putValue()">Btn1</button>
                <button type="submit" name="btn2" id="btn2" onclick="putValue()">Btn2</button>
    </form>

            <script type="text/javascript">
                function putValue(){
                    document.getElementById('btn1').value=document.getElementById('input').value;
                    document.getElementById('btn2').value=document.getElementById('input').value;
                }
            </script>