0

在我的 Windows 窗体上有 2 个 RadioButtons 和 2 个 TextBox,例如 rdbMale 和 rdbFemale 、 txtMale 、 txtFemale ,与我的 HTML 页面上的 Radio Button 和 TextBox 类似。在 Html 页面上,我在第一次加载 dom 时使用了一些 Jquery 我在单击男性单选按钮 rdbMale 时隐藏了两个 TextBoxes(txtMale,txtFemale),然后显示文本框 txtMale 并隐藏 txtFemale,类似于单击 rdbFemale 然后文本框 txtMale是隐藏,txtFemale 是显示。

我想从我的 windows 窗体中选择单选按钮的选择操作。当我单击单选按钮,例如 rdbMale 或 rdbFemale 按钮(在 winform 中也有 1 个按钮 SendToHtml)时,当我单击此 SendToHtml 时,单选按钮的相应值将传输到 HTML 页面(没有 ant 查询字符串等),我的 Jquery 事件将处理但在我的情况下,值单选按钮检查值传输到 HTML 页面,但 Jquery 无法显示和隐藏 txtMale 和 txtFemale。

HTML 代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test Application</title>
    <script type="text/javascript" language="javascript">
        window.onload = body_Onload;
        function body_Onload() {
            document.getElementById("txtFirstName").focus();
        }

    </script>
    <script type="text/javascript" src="../App_Script/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#txtMale,#txtFemale").hide();
            $("input[name=Sex]").click(function () {
                if ($("#radMale").attr('checked')) {
                    $("#txtMale").show();
                    $("#txtFemale").hide();
                }
                else {
                    $("#txtMale").hide();
                    $("#txtFemale").show();
                }
            });
        });
    </script>
</head>
<body>
<input id="radMale" type="radio" name="Sex" value="radMale" />
    <label for="lblMale">
        Male</label>
    &nbsp;&nbsp;
    <input id="radFeMale" type="radio" name="Sex" value="radFeMale" />
    <label id="lblFeMale">
        Female</label><br />
    <input type="text" id="txtMale" /><br />
    <input type="text" id="txtFemale" /><br />
</body>
</html>

C# 类

private SHDocVw.InternetExplorer TargetIE = null;
        string url;
public Form1()
        {
            InitializeComponent();
        }
private void Form1_Load(object sender, EventArgs e)
        {
            txtMale.Visible = false;
            txtFemale.Visible = false;
        }
private void SendToHtml_Click(object sender, EventArgs e)
        {
            GetTheIEObjectFromSystem("Q_26773800");
            SendTextToActiveElementWithSubmitOptionSet(false);
        }
private void GetTheIEObjectFromSystem(string inurl = ".")
        {
            SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows();
            foreach (SHDocVw.InternetExplorer internetExplorer in SWs)
            {
                if (internetExplorer.Document is mshtml.HTMLDocument)
                {
                    url = internetExplorer.LocationURL;
                    TargetIE = internetExplorer;
                    return;
                }
            }
        }

private void SendTextToActiveElementWithSubmitOptionSet(bool btnSubmit)
        {
            mshtml.IHTMLDocument2 document = null;
            if (TargetIE != null)
            {
                document = TargetIE.Document as mshtml.IHTMLDocument2;
                if (!document.activeElement.isTextEdit)
                {
                    MessageBox.Show("Active element is not a text-input system");
                }
                else
                {
                    HTMLInputElement HTMLI;
                    HTMLI = document.activeElement as HTMLInputElement;
                    var tag = HTMLI.document as mshtml.HTMLDocumentClass;
                    mshtml.IHTMLElementCollection hTMLElementCollection = tag.getElementsByTagName("input");
foreach (mshtml.HTMLInputElement el in hTMLElementCollection)
                    {
                        switch (el.id)
                        {
                case "radMale":
                                if (radioButton1.Checked == true)
                                    el.setAttribute("checked", "checked");
                                else
                                    el.removeAttribute("checked");
                                break;
                            case "radFeMale":
                                if (radioButton2.Checked == true)
                   el.setAttribute("checked", "checked");                                       
                                else
                                    el.removeAttribute("checked");
                                break;
                        }
                    }
 }
            }
            else
            {
                MessageBox.Show("open internet explorer.");
            }
        }
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtMale.Visible = true;
            txtFemale.Visible = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtFemale.Visible = true;
            txtMale.Visible = false;
        }

当从 winform 选择单选按钮到 Html Page 并从 winform 触发我的 Jquery 事件时我需要做什么

4

1 回答 1

1

我解决了我的问题 我只是粘贴了解决我的问题的代码而不是整个代码

                           case "radMale":
                                if (radioButton1.Checked == true)
                                {
                                    el.setAttribute("checked", "checked");
                                    el.click();
                                }
                                else
                                    el.removeAttribute("checked");
                                break;
                            case "radFeMale":
                                if (radioButton2.Checked == true)
                                {
                                    el.setAttribute("checked", "checked");
                                    el.click();
                                }
                                else
                                    el.removeAttribute("checked");
                                break;

或者这样

                       case "radFeMale":
                        if (radioButton2.Checked == true)
                        {
                            elInput.setAttribute("checked", true);
                            elInput.click();
                        }
                        else
                            elInput.setAttribute("checked", false);
                        break;

                    case "txtMale":
                        elInput.value = textBox5.Text;
                        break;

                    case "txtFemale":
                        elInput.value = textBox6.Text;
                        break;
于 2013-09-06T11:28:00.417 回答