弹出窗口时打开焦点的window.open弹出文本字段

时间:2013-12-02 06:34:02

标签: javascript jquery html

我有一个打开弹出窗口的页面

<script>
var popup = window.open('forms/trucksform.php');
popup.moveTo(0,0);
popup.resizeTo(318,340);
popup.focus();
popup.moveBy(500,200);
</script>

我如何专注于此弹出窗口中的文本字段

   <div class="mainform">
        <form name="addtrucks" id="addtrucks" method="POST" action="<?php echo  $editFormAction; ?>">
            <label>Truck Plate Number:</label>
            <input type="text" name="truck_plate_no" id="truck_plate_no" />
            <br/>
            <input name="button" type="submit" id="button" onclick="MM_validateForm('truck_plate_no','','R');return document.MM_returnValue"/>
            <input type="hidden" name="MM_insert" value="add" />
        </form>
    </div>

2 个答案:

答案 0 :(得分:1)

您可以使用以下两种方式: 从父页面,您可以访问弹出窗口:

<html>
<body>
<script>
var popup = window.open('trucksform.php');
popup.onload = function() {
    popup.document.getElementById("truck_plate_no").focus();
};
popup.moveTo(0,0);
popup.resizeTo(318,340);
popup.focus();
popup.moveBy(500,200);

</script>
</body>
</html>

或者将初始状态添加到弹出窗口(这样更好,因为你将在同一页面上引用元素):

 <div class="mainform">
        <form name="addtrucks" id="addtrucks" method="POST" action="<?php echo  $editFormAction; ?>">
            <label>Truck Plate Number:</label>
            <input type="text" name="truck_plate_no" id="truck_plate_no" />
            <br/>
            <input name="button" type="submit" id="button" onclick="MM_validateForm('truck_plate_no','','R');return document.MM_returnValue"/>
            <input type="hidden" name="MM_insert" value="add" />
        </form>
    </div>
<script>
   document.getElementById("truck_plate_no").focus();   
</script>

Chrome更新 看起来弹出窗口的操作阻止了一些UI功能。您可以在弹出窗口中添加文本输入属性autofocus="autofocus"。要使用弹出窗口删除操作,请使用open

的参数
<html>
<body>
<script>
var popup = window.open('trucksform.php', 'myWindowName', 'left=200,top=500,width=318,height=340');
</script>
</body>
</html>

答案 1 :(得分:0)

尝试

$("#truck_plate_no").focus();