简体   繁体   English

如何根据用户输入动态创建我的html表单的动作属性(URL)

[英]how to dynamically create the action attribute (URL) of my html form based on user input

Sorry - still a beginner - How do I dynamically create action url using the input text from the user? 抱歉 - 仍然是初学者 - 如何使用用户的输入文本动态创建操作URL?

 <form name="myInstitution"  action="" method="GET">
        <div>
            <label for="names" class="bold">Institution Name</label>
            <input id="names" name="schoolName" type="text"></input>                

        </div>

        <div class="bold"></div>

        <div>
            <input type="submit" value="Submit" id="sbutton" name="submit" />
            <input type="reset" value="Reset" />
        </div>
    </form>
$('#myForm').submit(function ()
{
    var action = '';
    // compute action here...
    $(this).attr('action', action);
});

Hope it will help you.. 希望它能帮到你..

Since you don't seem to be using a javascript library, I'll copy the bind code in pure javascript from this answer . 由于您似乎没有使用javascript库,我将从此答案中复制纯JavaScript中绑定代码

var on = (function(){
    if ("addEventListener" in window) {
        return function(target, type, listener){
            target.addEventListener(type, listener, false);
        };
    }
    else {
        return function(object, sEvent, fpNotify){
            object.attachEvent("on" + sEvent, function(){
                fpNotify(window.event);
            });
        };
    }
}());


on(document.getElementById("sbutton"), "click", function(){
    this.action = document.getElementById( "names" ).val() + ".ext";
});

If you were using a javascript library like jQuery , you could bind to the event handler like this: 如果你使用像jQuery这样的javascript库,你可以像这样绑定到事件处理程序:

$( "form[name=\"myInstitution\"]" ).on( "submit", function( e ){
    var $this = $(this),
        text  = $this.find( "input#names" ).val();

    $this.prop( "action", text + ".ext" );
});

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

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