如果条件为真,为什么Javascript window.location不能重定向页面?

时间:2016-09-05 06:18:33

标签: javascript forms login window.location

我刚刚开始使用javascript,这是为了测试。 我有基本的登录表单,用户名和密码,并添加一些Javascript到提取用户名和密码值,检查它们,如果它的真实应该重定向到另一个HTML页面。
在这种情况下,我使用window.location ..

以下是代码



 UITextField *CatpickerField = [[UITextField alloc] initWithFrame:self.categoryView.bounds];
CatpickerField.backgroundColor = [UIColor clearColor];
[[CatpickerField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];


CatpickerField .textAlignment = NSTextAlignmentRight;
CatpickerField.text= @"Category";
UIPickerView * pickerCAt = [UIPickerView new];
pickerCAt.backgroundColor = [UIColor whiteColor];
pickerCAt.tag=1001;
pickerCAt.delegate = self;
pickerCAt.dataSource = self;
pickerCAt.showsSelectionIndicator = NO;

UIToolbar *toolBarcat= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar1 setBarStyle:UIBarStyleBlack];
UIBarButtonItem *buttonCancelcat=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain  target:self action:@selector(barButtonCancelAction:)];
UIBarButtonItem *buttonDonecat=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain  target:self action:@selector(barButtonAction:)];
UIBarButtonItem *flexiblecat = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBarcat.items = [NSArray arrayWithObjects:buttonCancelcat,flexiblecat,buttonDonecat,nil];

CatpickerField.inputView = pickerCAt;
CatpickerField.inputAccessoryView = toolBar1;

self.CatPickerviewField = CatpickerField;
[self.categoryView addSubview:self.CatPickerviewField];

function Validate() {
		username = document.login.korisnicko.value;
		password = document.login.lozinka.value;
		
		if (username == "" || password == "") {
			window.alert ("Ne valja");
			return false;
		}
		else if (password.length < 6) {
			window.alert("mora biti duze od 6 slova");
			return false;
		}
		else {
			window.location = "profil.html";
			return true;
		}
	}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

这是一个工作小提琴: http://jsfiddle.net/uLbycrhm/1/

我不建议使用window.location代替正确的location.href,我也不建议使用window.alert代替alert({ {3}}),所以我也做了调整。此处使用return也是不必要的,因为您的功能与onclick相关联,而不是onsubmit

了解location是一种对象非常重要,以下是供您参考的链接:what's the difference?

答案 1 :(得分:0)

你可以用return关键字调用函数

<script>
function Validate() {
        username = document.login.korisnicko.value;
        password = document.login.lozinka.value;

        if (username == "" || password == "") {
            alert ("Ne valja");
            return false;
        }
        else if (password.length < 6) {
            alert("mora biti duze od 6 slova");
            return false;
        }
        else {
            window.location = "profil.html";
            return false;
        }
    }
</script>
<form name = "login" method="REQUEST" action=>
    <h1 id="greska"></h1>
    <p id="greska2"></p>
    <label>Korisnicko</label> <br>
    <input type ="text" id="korisnicko" name="korisnicko"> <br>
    <label>Lozinka</label> <br>
    <input type ="password" id="lozinka" name="lozinka"> <br>

    <button type = "submit" onclick="return Validate();">Submit</button>
</form>