简体   繁体   English

在表单 action= 中使用 javascript 和 html 表单输入

[英]use javascript and html form input in form action=

I have a simple form.我有一个简单的表格。 A user inputs text and I need that text to be used in the action attribute of form.用户输入文本,我需要在表单的 action 属性中使用该文本。 I also need to use a JavaScript variable in the form attribute action .我还需要在表单属性action使用 JavaScript 变量。 The other thing I need is to be able to test the form input to make sure it's ok.我需要的另一件事是能够测试表单输入以确保它没问题。

<script>
function validate(form){
var formVal=document.forms["myForm"]["test"].value;

simple test code for formVal

return true;
}


var scrt_var=737; //I've got more code to get this variable, I've just hardcoded it for simplicity
</script>

<FORM name="myForm" method="POST" action='http://www.example.com/viewdetails.php?id='+how to get scrt_var + '&page=' + how to get the text input; onsubmit="return validate(this);">

<input type="text" name="test" value="">
<input type="submit" value="Submit">
</form>

So how do I go about getting the variable scrt_var in the action attribute and also the inputted text as well?那么如何在action属性中获取变量scrt_var以及输入的文本呢?

This is simple event listener attached to the form onsubmit event.这是附加到表单onsubmit事件的简单事件侦听器。 It simply concatenates url string, input value and scrt_var and submits form to given url.它只是连接 url 字符串、输入值和scrt_var并将表单提交给给定的 url。

<script>
    var form=document.forms['myForm'];

    form.addEventListener('submit', function(){
        var testVal = this.elements['test'].value,
            scrt_var = 737;
        if(testVal){
            this.action = 'http://www.example.com/viewdetails.php?id=' + scrt_var + '&page=' + testVal;
            this.submit();
        }
    }, false);
</script>

    <form name="myForm" method="POST">
        <input type="text" name="test" value="">
        <input type="submit" value="Submit">
    </form>

I don't know what do you mean by statement: "The other thing I need is to be able to test the form input to make sure it's ok.".我不知道你所说的语句是什么意思:“我需要的另一件事是能够测试表单输入以确保它没问题。”。 "Ok input value" is very subjective and varies depending on what you want to achieve. “确定输入值”是非常主观的,取决于您想要实现的目标。 You have to try to validate it on your own (eg. check if the value is empty) ;)您必须尝试自己验证它(例如检查值是否为空);)

I think you should use jQuery.我认为你应该使用jQuery。 It is more simplier.它更简单。 :) :)

With jQuery:使用 jQuery:

function validiate(){
    //your validiation
    var testVal = $('#myForm #test').val();
    if(testVal == '' || testVal == ' '){
        alert('Not valid');   
    }else{
        $('#myForm').attr('action','yourUrl');
        $('body').append($('#myForm').attr('action'));
    }
    return true;
}

$('#myForm').submit(function(e){
    e.preventDefault();
    validiate();

Demo: http://jsfiddle.net/kxaAn/演示: http : //jsfiddle.net/kxaAn/

plain and simple干净利落

<script>
function validate(){
formVal = document.getElementById('test').value;
var scrt_var=737;
document.forms["myForm"].action='http://www.example.com/viewdetails.php?id='+ scrt_var + '&page=' + formVal;
}

</script>

<FORM name="myForm" method="POST" action='http://www.example.com/viewdetails.php?id='+how to get scrt_var + '&page=' + how to get the text input; onsubmit="validate();">

<input type="text" id="test" value="">
<input type="submit" value="Submit">
</form>

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

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