Nic*_*ell 0 html javascript jquery
我有一个链接到产品页面的按钮,就像这样......
<a href="/product.html" id="link"><button>Buy This</button></a>
Run Code Online (Sandbox Code Playgroud)
当我选中一个方框时,我需要更改href的值.这就是我到目前为止所拥有的
document.getElementById(#option1).checked = true;
document.getElementById(#link).value = '/product/option1.html';
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
谢谢!:)
当然它不起作用......
getElementById,不是GetElementById- 案件事!
'option1',不是#option1- 我不知道你认为哪个#option1是正确的.
您设置的checked属性true,这意味着该复选框将被托运设置.
您根本没有事件处理程序,因此此代码仅在页面加载时运行一次,并且在用户切换框时不会更新.
链接的href在其.href属性中,而不是它.value(它实际上没有.value)
你有一个标签<button>内部<a>.虽然大多数浏览器都会以您期望的方式运行,但在技术上并不合适.这不是一个保证,你应该在按钮上使用链接或JS事件处理程序.
考虑到所有这些,我向你呈现......
<button id="link">Buy this</button>
<label><input type="checkbox" id="option1" /> Option text</label>
Run Code Online (Sandbox Code Playgroud)
document.getElementById('link').onclick = function() {
if( document.getElementById('option1').checked) {
location.href = '/product/option1.html';
}
else {
location.href = '/product.html';
}
};
Run Code Online (Sandbox Code Playgroud)