Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果用户单击链接,如何聚焦下一个字段?_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 如果用户单击链接,如何聚焦下一个字段?

Javascript 如果用户单击链接,如何聚焦下一个字段?,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我有许多带有标签链接的字段,如下所示: <div class="control-group"> <label class="control-label" for="some-id-1"><a href="http://example.com/some-id-1" target="_blank">Text1</a></label> <div class="controls"> <input

我有许多带有标签链接的字段,如下所示:

<div class="control-group">
    <label class="control-label" for="some-id-1"><a href="http://example.com/some-id-1" target="_blank">Text1</a></label>
    <div class="controls">
        <input type="text" id="some-id-1" name="some-id-1"><br>
    </div>
</div>
<div class="control-group">
    <label class="control-label" for="some-id-2"><a href="http://example.com/some-id-2" target="_blank">Text2</a></label>
    <div class="controls">
        <input type="text" id="some-id-2" name="some-id-2"><br>
    </div>
</div>



如果用户单击链接,我如何根据字段聚焦(而不阻止默认操作)?也就是说,如果用户单击
Text1
,那么我应该打开
http://example.com/some-id-1
在新窗口中,使用id设置输入焦点
some-id-1


<a href="http://example.com/some-id-1" target="_blank" onClick="document.getElementById('some-id-1').focus();">Text1</a>


如果理解正确,您希望打开一个新页面,并在新页面中根据单击的链接设置焦点

如果您正在打开一个新页面,则需要在url中传递哈希或使用cookie,并让其他页面解析哈希或cookie

在当前页面中:

$(function(){
    $('.control-label a').click(function(){
       var hash='#'+ $(this).next().find('input').attr('id');
       window.open( this.href + hash);
        return false;
    });
})
在其他页面:

$(function(){
    var hash= location.hash;
    $(hash).focus();
})

一个页面中的JavaScript无法控制另一个页面如何响应URL,如果“其他”页面在您的控制下,则可以将其设置为解析
id

var parts = document.location.href.split('/'),
    id = parts.pop();

document.getElementById(id).focus;
另一方面,如果希望可靠地聚焦该元素,只需将
id
作为
hash
传递给URL即可:

<a href="somepage.html#some-id">Go to an element of 'some id'</a>


这可能不会聚焦表单元素,但它会引起用户的注意,并且在您无法控制的页面中,更可靠。

谢谢,但它不会打开链接。。。如果我删除了
preventDefault
,那么它不会聚焦该字段。谢谢,但它不会打开链接。。。但是它在
返回false时可以正常工作已删除。@如果您在当前页面上执行操作。。。OP希望打开新页面并设置焦点there@charlietfl我想OP应该打开新窗口,关注当前页面上的下一个输入。这个答案解决了这个问题。我想把重点放在第一页上。
var parts = document.location.href.split('/'),
    id = parts.pop();

document.getElementById(id).focus;
<a href="somepage.html#some-id">Go to an element of 'some id'</a>