VBScript onClick和jQuery Button Click

时间:2012-11-28 22:37:29

标签: jquery vbscript

我有一个带有一点VBScript的页面,当单击该按钮时会启动打印机安装。脚本成功完成后会发出警报。

我现在正尝试添加另一个按钮点击事件(可能使用jQuery),通知用户打印机安装已启动(按下按钮)。这是因为安装打印机可能需要20秒到1分钟。

实现以下代码时,我收到一个错误,即jQuery代码块中需要一个对象

   <script type="text/vbscript">
        function AddP(pName)
            Set WshNetwork = CreateObject("Wscript.Network")
            WshNetwork.AddWindowsPrinterConnection pName
            MsgBox "Printer Added"
        end function
   </script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready( function (){ 

            $("#btn").click( function(){

                 alert("Printer Install Started");

            });

         });
    </script>

 <td><a href="#" language="vbscript" onclick="AddP('\\PrinterName')"><input id="btn" name="Button1" type="button" value="Add"></a></td>

3 个答案:

答案 0 :(得分:1)

如果您想添加错误检查并提示设置为默认值。这是我第一次真实地编写脚本,所以请记住这一点。

<script type="text/vbscript">
function AddP(pName)

Shortname = split(pName, "\")

    If MsgBox("Click YES to install  " &ShortName(3),VBYesNo, "Printer Utility") = vbYes Then

        MsgBox "Installing Printer " &ShortName(3) &vbCrLf &vbCrLf &"This may take a few moments"


        Set WshNetwork = CreateObject("Wscript.Network")

        On Error Resume Next
        WshNetwork.AddWindowsPrinterConnection pName
            If Err.Number <> 0 Then

            MsgBox "Printer is not available for install." &vbCrLF &"Please contact Tech Support for assistance"
                Else If MsgBox ("Would you Like  " &ShortName(3) &" to be your Default Printer?" ,vbyesno, "Printer Utility")  = vbYes Then

                WshNetwork.SetDefaultPrinter pName
                MSgBox ShortName(3) &"  Installed and Set as Default",0,"Printer Utility"
                Else MSgBox ShortName(3) &"  Installed",0,"Printer Utility"

                End if

            End If

        On Error GoTo 0
     Else Msgbox "Printer Install Cancelled"
    End if
end function

&LT; / SCRIPT&GT;

答案 1 :(得分:0)

如果您想在添加打印机时向用户提供一些指示,可以更改按钮状态。

<script type="text/vbscript">
   function AddP(pName)
       Dim allButton1s
       Set allButton1s = document.getElementsByName("Button1")
       allButton1s(0).value = "Please wait..."
       Set WshNetwork = CreateObject("Wscript.Network")
       WshNetwork.AddWindowsPrinterConnection pName
       MsgBox "Printer Added"
       allButton1s(0).value = "Add"
   end function
</script>

答案 2 :(得分:0)

如果真的只是想在启动打印机之前发出警报,请在它之前添加另一个MsgBox行

<script type="text/vbscript">
   function AddP(pName)
       MsgBox "Printer Install Started"
       Set WshNetwork = CreateObject("Wscript.Network")
       WshNetwork.AddWindowsPrinterConnection pName
       MsgBox "Printer Added"
   end function
</script>

注意:如果您想避免使用按钮状态更改应答,则在用户在“打印安装启动”警报上按下确定之前,打印机安装才会启动。