【问题标题】:Pass radio button value into URL of onClick in form将单选按钮值传递到表单中 onClick 的 URL
【发布时间】:2013-04-14 12:10:19
【问题描述】:

我需要一些帮助才能使以下表单与单选按钮值一起使用。我希望用户选择一个类别(使用selectdrop down),然后选择一个大小(使用单选按钮),并在单击按钮时将类别的值和大小都传递到 URL 中。所以它会将用户带到http://thewebsiteaddress.com/category/size

除了单选按钮值之外,我在下面的基本表单工作正常 - 单击按钮时,它在 URL 中一直显示为“未定义”。我对此很陌生,如果能帮助我走上正确的道路或向我展示执行此任务的更好方法,我将不胜感激。

感谢您的指导。

<script type="text/javascript">
function combineSelections(cat,sizes){
location.href='http://thewebsiteaddress.com/product-category/'+cat.value+'/'+sizes.value;
}
</script>
<form name="searchbysize">
<select name="categories">
<option value="">Select Product Category</option>
<option value="category-slug">Category Name</option>
</select>
<input type="radio" id="very-small" name="size" value="very-small" />
<label for="very-small">Very Small </label>

<input type="radio" name="size" value="small" />
<label for="small">Small </label>

<input value="Search for Products" onclick="combineSelections(categories,size)"
type="button"></form>

【问题讨论】:

  • 未定义。在combineSelections(categories,size) 中,您还没有定义变量categoriessize

标签: javascript forms radio-button


【解决方案1】:

id="categories"添加到选择按钮,将id="size"添加到单选按钮,然后使用document.getElementById()获取单选按钮的值并选择。

function combineSelections()
{
   var size = document.getElementById('size').value;
   var category = document.getElementById('categories').value;
   location.href='http://thewebsiteaddress.com/product-category/'+category+'/'+size;
}

【讨论】:

  • 非常感谢 - 现在更有意义了,并且正确定义了变量,我终于让它完美地工作了。感谢您的帮助。
猜你喜欢
  • 2014-12-18
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多