如何根据答案在Javascript表单中生成链接 [英] How do I make links generate in Javascript Form Depending on Answers

查看:51
本文介绍了如何根据答案在Javascript表单中生成链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据所选的图片生成此脚本以生成代码内的链接? http://jsfiddle.net/P74tn/1/



How do I get this script to generate the links inside of the code depending on what pictures are selected? http://jsfiddle.net/P74tn/1/

<!DOCTYPE html>
<html>
  
  <style>
    .radio div{
    background: url($image) no-repeat #EEE;
    height:50px;
    width: 50px;
    border: 2px solid #DDD;
    padding: 5px;
    float: left;
    margin-right: 10px;
    cursor: pointer;
    &.active{
        box-shadow: 0px 0px 2px 1px blue inset;
    }
}
  </style>

<body>

<script>
$('.radio div').on('click', function() {
    var $this   = $(this),
        $parent = $this.parent(),
        value   = $this.attr('value');

    $parent.children().removeClass('active');
    $this.addClass('active');
    $parent.attr('value', value);
}); 
function tryToMakeLink()
    {
        //get all selected radios
        var q1=document.querySelector('input[name="q1"]:checked');
        var q2=document.querySelector('input[name="q2"]:checked');
        var q3=document.querySelector('input[name="q3"]:checked');
        //make sure the user has selected all 3
        if (q1==null || q2==null ||q3==null)
        {
            document.getElementById("linkDiv").innerHTML="<input type=button disabled=disabled value=Next>";
        }
        else
        {
            //now we know we have 3 radios, so get their values
            q1=q1.value;
            q2=q2.value;
            q3=q3.value;
            //now check the values to display a different link for the desired configuration
            if (q1=="AT&T" && q2=="8GB" && q3=="Black")
            {
              document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://google.com/';\">att 8gb black</input>";
            }
            else if (q1=="Other" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://yahoo.com/';\">other 8b white</input>";
            }
            else if (q1=="AT&T" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://bing.com/';\">another option</input>";
            }
            else if (q1=="AT&T" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://gmail.com/';\">oops</input>";
            }
            else if (q1=="AT&T" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://hotmail.com/';\">can't</input>";
            }
            else if (q1=="Other" && q2=="8GB" && q3=="Black")
            {
              document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://images.google.com/';\">yours</input>";
            }
            else if (q1=="Other" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://youtube.com/';\">mines</input>";
            }
            else if (q1=="Other" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://docs.google.com/';\">what</input>";
            }
            else if (q1=="Unlocked" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://wepriceit.webs.com/';\">red</input>";
            }
            else if (q1=="Unlocked" && q2=="8GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://webs.com/';\">orange</input>";
            }
            else if (q1=="Unlocked" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://gazelle.com/';\">green</input>";
            }
            else if (q1=="Unlocked" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://glyde.com/';\">blue</input>";
            }
        }
    }function tryToMakeLink()
    {
        //get all selected radios
        var q1=document.querySelector('input[name="q1"]:checked');
        var q2=document.querySelector('input[name="q2"]:checked');
        var q3=document.querySelector('input[name="q3"]:checked');
        //make sure the user has selected all 3
        if (q1==null || q2==null ||q3==null)
        {
            document.getElementById("linkDiv").innerHTML="<input type=button disabled=disabled value=Next>";
        }
        else
        {
            //now we know we have 3 radios, so get their values
            q1=q1.value;
            q2=q2.value;
            q3=q3.value;
            //now check the values to display a different link for the desired configuration
            if (q1=="AT&T" && q2=="8GB" && q3=="Black")
            {
              document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://google.com/';\">att 8gb black</input>";
            }
            else if (q1=="Other" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://yahoo.com/';\">other 8b white</input>";
            }
            else if (q1=="AT&T" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://bing.com/';\">another option</input>";
            }
            else if (q1=="AT&T" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://gmail.com/';\">oops</input>";
            }
            else if (q1=="AT&T" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://hotmail.com/';\">can't</input>";
            }
            else if (q1=="Other" && q2=="8GB" && q3=="Black")
            {
              document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://images.google.com/';\">yours</input>";
            }
            else if (q1=="Other" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://youtube.com/';\">mines</input>";
            }
            else if (q1=="Other" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://docs.google.com/';\">what</input>";
            }
            else if (q1=="Unlocked" && q2=="8GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://wepriceit.webs.com/';\">red</input>";
            }
            else if (q1=="Unlocked" && q2=="8GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://webs.com/';\">orange</input>";
            }
            else if (q1=="Unlocked" && q2=="16GB" && q3=="White")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://gazelle.com/';\">green</input>";
            }
            else if (q1=="Unlocked" && q2=="16GB" && q3=="Black")
            {
                document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick=\"window.location.href='http://glyde.com/';\">blue</input>";
            }
        }
    }
</script>

<form name="quiz" id='quiz'>
  
  
  <h4>What carrier do you have?</h4>
    <div class="radio" id="form">
      <div style="list-style: none;"  önclick=tryToMakeLink(); name="q1" value="AT&T"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
        <div style="list-style: none;"  önclick=tryToMakeLink(); name="q1" value="Other"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
        <div style="list-style: none;"  önclick=tryToMakeLink(); name="q1" value="Unlocked"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
    </div>
      
  <br /><br /><br />
      
  <h4>What is your phones capicity?</h4>
    <div class="radio" id="form">
        <div style="list-style: none;" type="radio"  önclick=tryToMakeLink(); name="q2" value="8GB"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
        <div style="list-style: none;" type="radio"  önclick=tryToMakeLink(); name="q2" value="16GB"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
    </div>

  <br /><br /><br />
      
  <h4>What color is your phone?</h4>
    <div class="radio" id="form">
        <div style="list-style: none;" type="radio"  önclick=tryToMakeLink(); name="q3" value="Black"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
        <div style="list-style: none;" type="radio"  önclick=tryToMakeLink(); name="q3" value="White"><img width="50px" height="50px" src="http://wepriceit.webs.com/ipad-5-image.jpg" /></div>
    </div>
      
    

    <br>
    <div id=linkDiv>
     <input type=button disabled=disabled value=Next>
    </div>
</form>
</body>
</html>

推荐答案

image)no-repeat #EEE;
身高:50px;
宽度:50px;
border:2px solid #DDD;
填充:5px;
float:left;
保证金权利:10px;
cursor:pointer;
& .active {
box-shadow:0px 0px 2px 1px blue inset;
}
}
< / style>

< body>

< script>
image) no-repeat #EEE; height:50px; width: 50px; border: 2px solid #DDD; padding: 5px; float: left; margin-right: 10px; cursor: pointer; &.active{ box-shadow: 0px 0px 2px 1px blue inset; } } </style> <body> <script>


('。radio div')。on('click',function(){
var
('.radio div').on('click', function() { var


this =


这篇关于如何根据答案在Javascript表单中生成链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆