简体   繁体   English

从div提取文本

[英]Extract text from a div

I am trying to make a DropDownList using divs and jquery (so that I can style it as I want)...and its working but the problem is I cant get the selected value from the list.. 我正在尝试使用div和jquery制作一个DropDownList(以便我可以根据需要设置其样式)...并且可以正常工作,但问题是我无法从列表中获取选定的值。

After selection of the option I am copying the selected value into the a div.. and I want to extract this using c# (in .aspx.cs page)... I've tried to do it using string builder and innerHtml(after adding runat="server" to the div).. but it doesn't work ...code is as follows 选择选项后,我将选择的值复制到div中。我想使用c#(在.aspx.cs页中)提取此值...我尝试使用字符串生成器和innerHtml(之后将runat =“ server”添加到div)..但是不起作用...代码如下

.aspx Page: .aspx页:

<div class="ddl">
    <div id="lowertriangle" class="lowertriangle"></div>
    <div id="uppertriangle" class="uppertriangle"></div>
    <div id="label" class="labeldiv_dd" runat="server"></div>//***This is the div from which I want to extract value***
    <div id="options" class="optionsidv_dd">
        <ul id="options_ul">
            <li id="0">Select One Option</li>
            <li id="1">Option 1</li>
            <li id="2">Option 2</li>
            <li id="3">Option 3</li>
            <li id="4">Option 4</li>
            <li id="5">Option 5</li>
        </ul>
    </div>
</div>

aspx.cs page aspx.cs页面

Method 1 that I tried: 我尝试的方法1:

string sel_text = label.InnerHtml;
display_sel_value.Text = sel_text.ToString(); 

2nd method: 第二种方法:

var sb = new StringBuilder();
label.RenderControl(new HtmlTextWriter(new StringWriter(sb)));

string s = sb.ToString();

Kindly point out my mistakes and help me in this regard(in extracting innerHTML of the div that is). 请指出我的错误,并在这方面帮助我(提取div的innerHTML)。 Thanks 谢谢

No, putting content in a div won't work. 不,将内容放在div中是行不通的。

Your example isn't complete enough to see all that happens, but let's assume that you're in a standard <form> , you're setting the div's inner HTML to a value with Javascript and then you're submitting in the standard way. 您的示例不够完整,无法看到所有发生的情况,但是假设您使用的是标准<form> ,使用Javascript将div的内部HTML设置为一个值,然后以标准方式提交。

Then one way to do what you want is to use a hidden input and setting its value attribute instead of its contents. 然后,一种执行所需操作的方法是使用隐藏的输入并设置其value属性而不是其内容。

<input id="label" class="labeldiv_dd" runat="server" type="hidden">

In the codebehind, the C# can retrieve the value from this control after submitting with the .Value property. 在后面的代码中,C#可以使用.Value属性提交后从此控件中检索值。

Ok guys thx for your replies..I found a way around the problem...I used HiddenField control to store the selected value using jQuery like so 好的,谢谢您的回复。.我找到了解决问题的方法...我使用HiddenField控件使用jQuery这样存储选定的值,如下所示

$("#options_ul li").click(function () {
            var text = this.innerHTML;
            ***$('#<%= selectedvalue.ClientID %>').val(text);***
            $("#options_ul li").css("background-color", "#c2c2c2");
            $(this).css("background-color", "white");
            //var prev = this.id;
            //document.getElementById("label").innerHTML = text;
            toggleHeight();
        });

and then accessed it server side using 然后使用访问服务器端

selectedvalue.value;

PS: "selectedvalue" is id of the hiddenfield control PS:“ selectedvalue”是hiddenfield控件的ID

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

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