我有一个注册页面,用户输入该页面时,在url上添加一个#标记,如
mysite.com/registration#johnjackson
根据单击哪个链接进入注册页,将添加hashtag。字符串总是小写和裁剪的。
在注册页面上有一个下拉列表,我希望通过使用js来预选它。它看起来像是:
<select>
<option value="Sara Rush, Singer">Sara Rush, Singer</option>
<option value="John Jackson, painter">John Jackson, painter</option>
<option value="Albert Einstein, Pilot">Albert Einstein, Pilot</option>
</select>
我在找一个很好的解决办法。也许只是查看选项值列表,检查前5-7个字符是否与hashtag字符串匹配?
发布于 2013-08-30 02:19:07
像这样的东西怎么样。
var hash = window.location.hash;
var regex = new RegExp( hash, 'g' );
$('option').each(function() {
var val = $(this).attr('value').toLowerCase();
var matches = val.match(regex);
if (matches) {
$(this).attr('selected', 'selected');
}
});
https://stackoverflow.com/questions/18523691
复制相似问题