简体   繁体   English

通过javascript提示获取信息以获取表格

[英]Getting information for get form via javascript prompt

*

<form id="5" form method="get" action="http://crimson-craft.buycraft.net/checkout/packages">
    <input type="hidden" name="direct" value="true">
    <input type="hidden" name="action" value="add">
    <input type="hidden" name="package" value="1250806">
    <input type="hidden" name="ign" value=username>
</form>
<script>
    function buynow5() {  
    var username = prompt("Please enter your MineCraft username", "");  
    if (username != null) {
            document.getElementById("5").submit();
        }
    }
</script>
<a href="javascript:void(0);" onclick="buynow5()" class="sign_up radius3">Buy Now</a>

* *

So the java-script popup works and asks for a input, then submits the form, but the form doesn't pass the variable. 因此,java脚本弹出窗口可以工作并要求输入,然后提交表单,但表单不传递变量。 I'm sorry if this a dumb question, but I'm very new to this. 很抱歉,这是一个愚蠢的问题,但是我对此很陌生。

Fix: 固定:

Change this: 更改此:

<input type="hidden" name="ign" value=username>

to this: 对此:

<input id="usernameInput" type="hidden" name="ign" value="">

Then update your method like so: 然后像这样更新您的方法:

function buynow5() {  
    var username = prompt("Please enter your MineCraft username", "");  
    if (username) {
        document.getElementById('usernameInput').value = username;
        document.getElementById('5').submit();
    }
}

You can't bind javascript properties to HTML inputs without extra libraries. 没有额外的库,您就无法将javascript属性绑定到HTML输入。

Tip: 小费:

While you can make this work without any libraries, I highly suggest you look in to jQuery to facilitate DOM manipulation and interactive events. 虽然您可以在没有任何库的情况下完成此工作,但我强烈建议您使用jQuery来促进DOM操作和交互事件。 If you want more control and databinding, take a look at AngularJS as well. 如果您想要更多的控制和数据绑定,也可以看看AngularJS

Full Code: 完整代码:

<form id="5" form method="get" action="http://crimson-craft.buycraft.net/checkout/packages">
    <input type="hidden" name="direct" value="true">
    <input type="hidden" name="action" value="add">
    <input type="hidden" name="package" value="1250806">
    <input id="usernameInput" type="hidden" name="ign" value="">
</form>
<script>
    function buynow5() {  
    var username = prompt("Please enter your MineCraft username", "");  
    if (username) {
            document.getElementById('usernameInput').value = username;
            document.getElementById("5").submit();
        }
    }
</script>
<a href="javascript:void(0);" onclick="buynow5()" class="sign_up radius3">Buy Now</a>

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

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