2010-09-01 61 views
1

好吧,所以我完全新的JavaScript,但我需要做以下事项:从标签ID到使用JavaScript形式?

我的页面我有选项卡(用JavaScript完成),然后我有搜索表单。现在我需要从这些标签中选择一个标识来表示这种形式。

我有tabs1 tabs2 tabs3和所有这些href =“#1”,href =“#2”等等。我需要从该选项卡中获取该数字以形成输入值。所以如果使用点击Tab2,它会自动将“2”作为我的表单中的一个输入值。怎么做?谢谢!

回答

1

释在HTML注释:

<!-- retrieves the value from href, skipping the first character --> 
<a href="#1" onclick="document.forms.form_name.field_name.value=this.hash.substr(1)">Tab1</a> 
<form name="form_name" action="" method="post"> 
<!-- defaults to tab 1 --> 
<input type="hidden" name="field_name" value="1" /> 

</form> 
+2

'getAttribute'不能正常工作在IE,你会得到解决的URL一样,如果你使用'this.href'。所以你最终会用'ttp:// something /#2'。 getAttribute' /'setAttribute'几乎不应该用在HTML文档中;在这里,我建议使用'this.hash.slice(1)'。 – bobince 2010-09-01 17:15:10

+0

我用getAttribute获取* real *属性值。这在Firefox中起作用,但正如您所指出的那样,它在IE中不起作用。很好的解决方案,使用Link.hash :)你能解释为什么你选择'.slice(1)'而不是'.substr(1)'吗? – Lekensteyn 2010-09-01 17:17:28

+0

@Lekensteyn参见http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring-in-javascript – 2011-10-25 15:10:46

相关问题