链接未生成
Links aren't generating
我有一个表单,但我无法生成我的链接。下一个按钮应该亮起,然后是一些文本。
它应该如何工作:http://jsfiddle.net/zMQcn/
不起作用的那个:http://jsfiddle.net/Yq8Qf/
document.getElementById("linkDiv").innerHTML="<input type=button value=Next onclick='"window.location.href='http://yahoo.com/';'">other 8b white</input>";
}
根据我上面的评论
<form name="quiz" id='quiz'>
<h4>What carrier do you have?</h4>
<select style="margin-top: 1pt" onchange="tryToMakeLink()" name="q1">
<option disabled="disabled" selected>Choose your model</option>
<option value="AT&T">AT&T</option>
<option value="Other">Other</option>
<option value="Unlocked">Unlocked</option>
</select>
<h4>What is your phones capicity?</h4>
<select style="margin-top: 1pt" onchange="tryToMakeLink()" name="q2">
<option disabled="disabled" selected>Choose your model</option>
<option value="8GB">8GB</option>
<option value="16GB">16GB</option>
</select>
<h4>What color is your phone?</h4>
<select style="margin-top: 1pt" onchange="tryToMakeLink()" name="q3">
<option disabled="disabled" selected>Choose your model</option>
<option value="Black">Black</option>
<option value="White">White</option>
</select>
<br />
<div id=linkDiv>
<input type=button disabled=disabled value=Next />
</div>
</form>
和
function tryToMakeLink() {
//get all selected radios
var q1 = document.querySelector('select[name="q1"]');
var q2 = document.querySelector('select[name="q2"]');
var q3 = document.querySelector('select[name="q3"]');
//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 {
console.log(q1, q1.selectedIndex)
//now we know we have 3 radios, so get their values
q1 = q1.selectedIndex > 0 ? q1.options[q1.selectedIndex].value : '';
q2 = q2.selectedIndex > 0 ? q2.options[q2.selectedIndex].value : '';
q3 = q3.selectedIndex > 0 ? q3.options[q3.selectedIndex].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>";
}
}
}
演示:小提琴
从<select>
获取值时,您必须做一些不同的事情。
将 HTML 更改为使用 onchange
,并为<select>
提供name
属性,而不是其选项。 样式设置应在标记之外进行。 元素还应具有唯一的 ID,或者在本例中没有 ID。
<select onchange="tryToMakeLink()" name='q1'>
<option disabled="disabled" selected>Choose your model</option>
<option value="AT&T">AT&T</option>
<option value="Other">Other</option>
<option value="Unlocked">Unlocked</option>
</select>
然后,使用查询选择器获取<select>
:
var q1 = document.querySelector('select[name="q1"]');
不是检查null
,而是检查selectedIndex
:
if (q1.selectedIndex && q2.selectedIndex && q3.selectedIndex) {
...enable button and proceed...
然后,在准备就绪时,使用 selectedIndex
获取选项的值。
q1 = q1.options[q1.selectedIndex];
然后像往常一样继续。
此外,如果您要将用户发送到新页面,良好的 UX(用户体验)实践决定了您应该使用 LINK,因为人们会期望一个链接来更改他们的浏览器。 如果他们点击一个按钮并转到一个新页面,那就不那么直观了。
更新
这是您的代码为ATT 8GB black
工作的小提琴。 不过我已经清理了一下。
HTML
<h4>What carrier do you have?</h4>
<select style="margin-top: 1pt" id="carrier">
<option value="">Choose your carrier...</option>
<option value="ATT">AT&T</option>
<option value="Other">Other</option>
<option value="Unlocked">Unlocked</option>
</select>
<h4>What is your phones capicity?</h4>
<select style="margin-top: 1pt" id="capacity">
<option value="">Choose your capacity...</option>
<option value="8">8GB</option>
<option value="16">16GB</option>
</select>
<h4>What color is your phone?</h4>
<select style="margin-top: 1pt" id="color">
<option value="">Choose your color...</option>
<option value="black">Black</option>
<option value="white">White</option>
</select>
<p>
<input disabled id="mybutton" type="button" value="Next"/>
</p>
爪哇语
将窗格中的第二个下拉列表从"onLoad
"更改为"No wrap - in <head>
"。我知道我没有使用完美的加载事件,但现在这有效。:p 所有 URL 都存储在 JSON 对象中,因此您可以更轻松地对其进行编辑。现在它只是在选择URL的情况下发出警报。
var URLs = {
'ATT': {
'8': {
'black': 'http://www.google.com/',
'white': 'http://www.yahoo.com/'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
},
'Other': {
'8': {
'black': 'http://www.google.com',
'white': 'http://www.yahoo.com'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
},
'Unlocked': {
'8': {
'black': 'http://www.google.com',
'white': 'http://www.yahoo.com'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
}
};
// check to see if all selects have a value that isn't the first one
function checkInput() {
var disable = false;
var select = document.getElementsByTagName('select');
for(var i=0,l=select.length;i<l;i++) {
if(select[i].selectedIndex==0)
{ disable = true; }
}
document.getElementById('mybutton').disabled = disable;
}
function go() {
var carrierE = document.getElementById('carrier');
var carrier = carrierE.getElementsByTagName('option')[carrierE.selectedIndex].value;
var capacityE = document.getElementById('capacity');
var capacity = capacityE.getElementsByTagName('option')[capacityE.selectedIndex].value;
var colorE = document.getElementById('color');
var color = colorE.getElementsByTagName('option')[colorE.selectedIndex].value;
window.alert(URLs[carrier][capacity][color]);
}
function init() {
var select = document.getElementsByTagName('select');
// attach checkInput to all select elements
for(var i=0,l=select.length;i<l;i++) {
if(select[i].addEventListener)
{ select[i].addEventListener('change',checkInput,false); }
else if(select[i].attachEvent)
{ select[i].attachEvent('onchange',checkInput); }
else
{ select[i].onchange = checkInput; }
}
var button = document.getElementById('mybutton');
if(button.addEventListener)
{ button.addEventListener('click',go,false); }
else if(button.attachEvent)
{ button.attachEvent('onclick',go); }
else
{ button.onclick = go; }
}
if(document.addEventListener) {
document.addEventListener('DOMContentLoaded',init,false);
}
else {
// I can do better but it's not important here
window.onload = init;
}
相关文章:
- 尽管链接成功并已成功下载,但未找到NPM模块
- jQuery mouseup在拖离链接后未启动
- 角度指令's链接函数未被调用
- 绝对链接/相对链接均未获胜't在菜单中工作
- LinkedIn共享链接图片未使用自定义url更新
- 从内部链接进入页面时,引导模式未打开
- 来自流星模板的 HTML 图像链接未由浏览器呈现
- 当某些内容发生变化时,如何将已访问的链接更改为未访问的链接
- 如何在shopify中使用谷歌分析来跟踪未绑定的链接
- 禁用触摸设备上的单击链接未按预期工作
- j查询在点击时未检测到链接
- 调用在Jquery中未作为链接函数工作
- 反应路由器,所需的道具“to”未在“链接”中指定
- click() 未在链接元素上调用
- SugarCRM新鲜库存安装样式和脚本未正确链接
- Ruby-on-Rails JavaScript 未在链接上加载
- 如何更改当前打开的链接的文本颜色,并保持未打开链接的默认颜色?这样用户就可以知道他现在在哪个链接上
- 使用jquery悬停时未显示链接
- JavaScript -当我更改链接的href时未定义链接
- 下拉菜单 - Javascript val() 用于选择框未正确链接