当访问者点击按钮元素时,如何更改URL?
例:
<form id="search">
<input type="text" />
<button onclick="javascript:alert('hi');">Alert Hi</button>
<button onclick="javascript:window.location.href='http://www.google.com/'">Go to Google</button>
</form>
Run Code Online (Sandbox Code Playgroud)
(或者看看JSFiddle.)
对于快速演示页面,我只需要在访问者点击按钮时将其发送到新的URL.我可以执行JavaScript onclick(在Alert Hi按钮中),如果我window.location.href='http://www.google.com/'从Firebug的控制台执行,则URL会发生变化.在点击按钮时更改URL需要做什么?
我有一个包含按钮的表单,单击按钮时会提交表单,以及如何防止这种情况。例如,我只想在单击按钮时显示“警报”。并将按钮放在表单之外对我来说不是一个选择。
顺便说一句,我使用引导框架。
<form class="form-inline" role="form" id="form-area">
<input type="hidden" name="key" value="dept-area">
<div class="form-group">
<input type="text" class="form-control" name="data" placeholder="">
</div>
<button class="btn btn-default" id="btn-area">test</button>
</form>
Run Code Online (Sandbox Code Playgroud) 我很确定这是我没有完全理解异步javascript的问题,但是我将解释我的问题。
我有一个通过Firebase认证的电子邮件/密码,并且我具有以下形式的登录页面:
<form class="form-signin" role="form" action="#" onsubmit="return login(this);">
<h2 class="form-signin-heading">Please log in</h2>
<input type="email" id="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" id="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Log in</button>
</form>
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,我的JS控制台中出现以下错误,该错误非常快地闪烁:
未捕获的TypeError:无法读取未定义的属性'token'--- firebase-simple-login.js:132
在head标签的底部,包括脚本“ js / firebase.js”,这是该文件:
var myRef = new Firebase("https://xxxx.firebaseio.com");
var auth = new FirebaseSimpleLogin(myRef, function(error, user) {
if (error) {
console.log(error);
} else if (user) {
console.log("User ID: " + user.uid + ", Provider: " + user.provider);
} else {
}
});
function …Run Code Online (Sandbox Code Playgroud) HTML:
<form id="form" action="/" method='post'>
<button>Button</button>
<input type="text">
<input type="submit" id="send-form" value="Send">
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$('#form').submit(function() {
console.log('send');
return false;
});
Run Code Online (Sandbox Code Playgroud)
为什么当我按下时Button,表单被提交(我需要这个元素进行另一个动作)?怎么预防这个?
我有一个带有输入文本字段和按钮的表单.我想捕获用户在文本字段中按Enter键 - 不提交表单,而是触发另一个操作.该按钮有自己独立的单击处理程序,当我在文本字段中按Enter键时,由于某种原因会被触发.
示例:http: //jsfiddle.net/T8zLq/1/
<form onsubmit="return false" ng-submit="mysubmit()" ng-controller="TestCtrl" ng-app="Test">
<input type="text" />
<button ng-click="test()">X</button>
</form>
var app=angular.module("Test", []);
app.controller("TestCtrl", ["$scope", function($scope) {
$scope.test = function() {alert('lol'); };
$scope.mysubmit = function() {alert('submit');};
}]);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我有一个小问题.
我正在学习Javascript,我决定制作货币转换器但是,我的页面在显示数据后仍然保持刷新.
任何人都可以帮我弄清楚为什么这样做?谢谢
网站:http://nonlocalhost.uphero.com/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
* {font-size:13px;font-family:arial;background-color:black;color:white;padding:0;margin:0;}
#wrapper {width:640px;margin:0px auto;}
input {color:lime;width:150px;height:22px;}
#money_to_convert, label:nth-child(3) {position:relative;top:100px;right:95px;}
#my_currency {width:53px;height:22px;position:relative;top:100px;left:232px;}
#converted_input, label:nth-child(5) {position:relative;top:134px;right:298px;}
#convert_currency {width:53px;height:22px;position:relative;top:134px;left:175px;}
#submit_button {width:52px;height:25px;position:relative;top:117px;right:230px;}
</style>
<script type="text/javascript">
function output_value() {
var my_input = Number(document.getElementsByName("user_input")[0].value);
var my_output;
var my_currency = document.convertions.currency_to_convert.value;
var convert_to = document.convertions.convert_currency_to.value;
if(my_currency == "USD"){
if(convert_to == "CAD"){
my_output = my_input * 0.975;
document.getElementsByName("convertion_output")[0].value = my_output;
}else if(convert_to == "EUR"){
my_output = my_input …Run Code Online (Sandbox Code Playgroud)