我想要区分用户输入的值。
用户使用输入表单来添加值。
如果'#‘是用户输入的值的前面。重定向到A
不包括重定向到B
我试图通过使用charAt()来区分这一点,但是不起作用。
我是第一次使用javascript,所以有很多错误。你能看看我的密码吗?
html/javascript
<form>
<input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">
<input onclick="return findSharp()" type="submit" value="Search" style="background:#222;">
</form>
<script>
function findSharp() {
var stringName = document.getElementById("pathID").value;
if (stringName.charAt(0) == '#') {
findURLA();
} else {
fidneURLB();
}
}
function findURLA() {
window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + document.getElementById("pathID").value;
return false;
}
function findURLB(){
window.location.href = 'http://localhost:8181/mainpage/' + '?' + 'activity/' + document.getElementById("pathID").value;
return false;
}
</script>发布于 2019-09-23 06:19:24
您的代码是正确的,但您使用的是type=“提交”。所以如果你把这个换成纽扣。你可以的。
另外,您输入了函数名错误。将其更改为findURLB
<form> <input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">
<input onclick="return findSharp()" type="button" value="Search" style="background:#222;"></form>
<script>
function findSharp(){
var stringName = document.getElementById("pathID").value;
if(stringName.charAt(0) == '#')
{
findURLA()
}
else
{
findURLB()
}
}
function findURLA(){
window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + document.getElementById("pathID").value;
return false;
}
function findURLB(){
window.location.href = 'http://localhost:8181/mainpage/' + '?' + 'activity/' + document.getElementById("pathID").value;
return false;
}
</script>
发布于 2019-09-23 06:25:51
我认为您需要验证您的URL
function isValidUrl(Url){
if(/^(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i.test(Url))
alert("URL is Valid");
else
alert("URL is Invalid");
}URL: <input id="Url" onchange="isValidUrl(this.value)" />
发布于 2019-09-23 06:26:49
使用输入类型作为对接on,而不是submit
如果要重定向,则直接将参数传递给函数。
<form> <input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">
<input onclick="return findSharp()" type="button" value="Search" style="background:#222;color:white;padding:10px"></form>
<script>
function findSharp(){
var stringName = document.getElementById("pathID").value;
if(stringName.charAt(0) == '#'){
findURLA(stringName)
}else{
findURLB(stringName)
}
}
function findURLA(data){
window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + data;
return false;
}
function findURLB(data){
window.location.href = 'http://localhost:8181/mainpage/' + '?' + 'activity/' + data;
}
</script>
https://stackoverflow.com/questions/58056450
复制相似问题