简体   繁体   English

如何获取客户端代码的html标签innerHTML?

[英]How to get a html tag innerHTML of client side code?

Using asp.net and c# on a sharepoint server I edit the innerhtml of a div tag that has runat="server", and I place lots of html code in it and an input box (this input box is the autocomplete jquery UI input box). 在共享点服务器上使用asp.net和c#,我编辑了具有runat =“ server”的div标签的innerhtml,并在其中放置了很多html代码和一个输入框(此输入框是自动完成的jquery UI输入框)。

I can give the input box an id. 我可以给输入框一个ID。

Now how can I access the text in the input box in c#? 现在如何在c#的输入框中访问文本?

        searchbox.InnerHtml = @"
                <script type='text/javascript'>
                    $(function() {
                        var availableTags = [
                            " + results + @"
                        ];
                        $( '#tags' ).autocomplete({
                            source: availableTags
                        });
                    });
                </script>
                <div class='demo'>
                    <div class='ui-widget'>
                        <input id='tags'>
                    </div>
                </div>";

You want an HTML parser. 您需要一个HTML解析器。 You can try out this library . 您可以尝试使用该库 Specifically, if you use Chrome.. right click the box itself and 'Inspect it'. 具体来说,如果您使用的是Chrome ..,请右键单击该框本身,然后单击“检查”。 Right click the inputbox, and hit 'Copy XPath'. 右键单击输入框,然后单击“复制XPath”。 This is the path, now just select the element with HTMLAgilityPack. 这是路径,现在只需使用HTMLAgilityPack选择元素。

You cannot directly access to the value of the input box from the server side code. 您不能直接从服务器端代码访问输入框的值。 You either need to post it back or send the value of the input box by an Ajax request to the server. 您需要将其回发或通过Ajax请求将输入框的值发送到服务器。

Post back using form & submit 使用表格回发并提交

<form action="yourpage.aspz" method="POST">
    <input type="text" name="inputbox" id="inputbox"/>
    <input type="submit" name="submit" value="submit"/>
</form>

And for the Ajax code with JQuery: 对于使用JQuery的Ajax代码:

<input type="text" name="inputbox" id="inputbox"/>
<input type="button" name="submit" value="submit" onclick="ajaxPost();"/>
<script type="text/javascript">
    function ajaxPost(){
        var inputBoxValue=$("#inputbox").val();
        $.post("yourpage.aspx", { inputbox: inputBoxValue } );
    }
</script>

Yourpage.aspx.cs Yourpage.aspx.cs

     // ... some other code 
     var a = Request.Form("inputbox")
     // ... some other code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM