当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - change redirect location onClick.

问题描述:

I'm trying to change a redirect link using check boxes that append to the URL and a button that uses the changed URL. Im still a beginner to JavaScript and am a little lost as to where i'm going wrong.

My code:

<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>

网友答案:

<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>
网友答案:

<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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: