简体   繁体   中英

transfer data from javascript popup multiline textbox to a select control

I am trying to transfer data from a multiline textbox to a select control. The multiline textbox appears as a popup and I want all the records pasted in the textbox to be transferred to the select control once the user will click submit in the popup window. Probably with jquery or javascript, or maybe something else. The page is built in MVC3 Razor. Here is the code from the page:

The script for the popup control:

<script type="text/javascript">
    $(function () {
        $("a[id^=opener]").click(function () {
                    $("#dialog").dialog('destroy');
                    $("#dialog").attr("title", "Please paste your products")
                    .html("<p><textarea name=\"TextMessage\" rows=\"10\" cols=\"72\" /><br /><input type=\"submit\" value=\"Submit\" /></p>");

                    $("#dialog").dialog({
                        height: 420,
                        width: 650,
                        modal: true
                    });
        });

    });
</script>

The .cshtml page:

@using (Html.BeginForm("ASPXView", "Report", FormMethod.Post)) {
    @Html.ValidationSummary(true, "Password change was unsuccessful. Please correct the errors and try again.")
    <div>
        @Html.Hidden("Id", Model.Report.Id)
        <div id="accordion">
        @{int i=0;}
            @foreach (var item in Model.Parameters)
            {
                <h3><a href="#">@Html.LabelFor(m => item.Name, item.Prompt)</a></h3>
                <div>
                    <div class="editor-label">
                        Search @*Html.TextBox("Search")*@ 
                        <input id="@("Search" + item.Name)" type="text" name="q" data-autocomplete="@Url.Action("QuickSearch/" + item.Name, "Report")" />
                    </div>

                    <div class="editor-field">
                        <select multiple id="@("Select" +item.Name)" name="@("Select" +item.Name)"></select>                           
                    </div>

                    <div class="removed" style="clear:both; float:left; margin-left:440px;">  
                     <a href="#" class="remove">Remove selection</a>   
                     <a id= "opener@(i)" class="OpenDialogClass" href="#" >Open Dialog</a>           
                    </div>    

                </div>
                i++;
            }         
        </div>
        <p style="text-align: right">
            <input type="submit" value="Generate Report" />
        </p>
    </div>
}
<div id="dialog" title="Basic dialog">
</div>

Screenshot from the page:

在此处输入图片说明

So the data which will be pasted in the popup textbox, I would like to have it in the select control once the submit button is clicked. Any idea how I can do that? Thanks in advance, Laziale

You could serialize the contents of the textarea and then do what you need to do with it (Post it to a controller or perhaps hand it off to the underlying page somewhere)

$('form').submit(function(e){
   e.preventDefault();
   e.stopPropagation();

   var o = {};
   $( $('textarea').val().split(/\n|\r/) ).each(function(i){
      o[i] = this;
    });   
    var jsonString = JSON.stringify(o);   

    // DO SOMETHING WITH JSON OBJECT HERE
});​

http://jsfiddle.net/vUH3a/

This will give you a start.

$("Button Selector").click(function(){
var SelectOptions= [];
        $("list Selector").each(function () {
            SelectOptions.push($(this).attr('id'));
        });
SelectOptions.each(function () {
            //build your mark up here and return
        });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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