创建按钮,根据表单域

时间:2016-05-12 06:09:20

标签: javascript html

我有两个输入表单字段,我希望当用户单击提交按钮时,他应该根据这两个字段中的输入将其带到URL。例如,如果两个输入字段中的输入分别是A和B,则应设置条件,以便在Javascript中将用户带到www.mydomain.com/C。我不希望将值附加到www.mydomain.com/a/b这样我已经知道如何的URL。

我已经在SO和Google上看到了很多关于URL生成的问题但是没有像我一样的情况。我非常感谢SO用户的帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

你是说这样的意思吗?当两个输入都具有值'something'并且单击按钮时,这会将您带到地址。

     - (IBAction)submit:(id)sender 
{
//Here YOUR URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my URL…..."]];




 //create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];

//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
 NSString *userUpdate =[NSString stringWithFormat:@"name=%@&email=%@&phone=%@&  comment=%@&",_txname.text,_txemail.text,_txcontect.text,_txcomment.text,nil];

//Check The Value what we passed
NSLog(@"the data Details is =%@", userUpdate);

  //Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];

  //Apply the data to the body
[request setHTTPBody:data1];

 //Create the response and Error
NSError *err;
NSURLResponse *response;

  NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

 //This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
    NSLog(@"got response");
 else
{
    NSLog(@"faield to connect");
}
}

答案 1 :(得分:0)

如果您使用的是jquery:

$( "form" ).submit(function() {
    // TODO read your variables
    // TODO apply conditions and redirect accordingly
    if ( ... ) {
        window.location = 'http://www.example.com/C'
    } else {
        window.location = 'http://www.example.com/D'
    }

    return false; // prevent default submit
});