更改重定向位置onClick。

时间:2017-02-04 07:33:27

标签: javascript html forms url web

我正在尝试使用附加到网址的复选框和使用更改后的网址的按钮来更改重定向链接。我仍然是JavaScript的初学者,对于我出错的地方我有点迷失。

我的代码:

<html>

<form>

<div class="element1">" 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 

<div class="element2">" 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 

<div class="element3">" 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div>


<div class="element00"> 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 

</form>

<script>
function calculatedUrl(){
  var totalString=""
  var link1="some text"
  var link2="more  text"
  var link3="something else"

   if(document.getElementById("id1").checked == true){
     var totalString = totalString.concat(link1);

}

 if(document.getElementById("id2").checked == true){
         var totalString = totalString.concat(link2);


}

 if(document.getElementById("id3").checked == true){
         var totalString = totalString.concat(link3);

}
  return totalString

}

function myFunction(id){
 var original="http://myoriginalurl"
 var addition=id
 var new=original.concat(addition)




//for testing
  window.alert(new)

  document.getElementById("test1").innerHTML= new;


//for the real redirecting not active for now
//window.location=new
                          }




</script>


</html>

2 个答案:

答案 0 :(得分:0)

<html>

<form>

<div class="element1">" 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 

<div class="element2">" 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 

<div class="element3">" 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div>


<div class="element00"> 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 

</form>

<script>
function calculatedUrl(){
  var totalString=""
  var link1="some text"
  var link2="more  text"
  var link3="something else"

   if(document.getElementById("id1").checked == true){
     var totalString = totalString.concat(link1);

}

 if(document.getElementById("id2").checked == true){
         var totalString = totalString.concat(link2);


}

 if(document.getElementById("id3").checked == true){
         var totalString = totalString.concat(link3);

}
  return totalString

}

function myFunction(id){
 var original="http://myoriginalurl"
 var addition=id
 console.log(id);
 var n =original.concat(addition)




//for testing
  window.alert(n)

  document.getElementById("test1").innerHTML= n;


//for the real redirecting not active for now
//window.location=new
                          }




</script>


</html>

目前,在您的代码更改中,newnewurl - new是保留字。

function myFunction(id){
var original="http://myoriginalurl"
var addition=id
var newurl =original.concat(addition)
//for testing
window.alert(newurl)
document.getElementById("test1").innerHTML= newurl;
//for the real redirecting not active for now
//window.location=new
}

我没有看到id=test1的标记,因此您将在控制台浏览器中获得null异常。根据该标签添加标签并进行测试。

我添加了一个代码段,请检查。

答案 1 :(得分:0)

<html>

<form>

<div class="element1">" 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 

<div class="element2">" 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 

<div class="element3">" 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div>


<div class="element00"> 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 

</form>

<script>
function calculatedUrl(){
  var totalString=""
  var link1="some text"
  var link2="more  text"
  var link3="something else"

   if(document.getElementById("id1").checked == true){
     var totalString = totalString.concat(link1);

}

 if(document.getElementById("id2").checked == true){
         var totalString = totalString.concat(link2);


}

 if(document.getElementById("id3").checked == true){
         var totalString = totalString.concat(link3);

}
  return totalString

}

function myFunction(id){
 var original="http://myoriginalurl"
 var addition=id
 console.log(id);
 var n =original.concat(addition)




//for testing
  window.alert(n)

  document.getElementById("test1").innerHTML= n;


//for the real redirecting not active for now
//window.location=new
                          }




</script>


</html>