transfer data from javascript popup multiline textbox to a select control

你说的曾经没有我的故事 提交于 2019-12-31 04:00:56

问题


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


回答1:


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/




回答2:


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
        });
});


来源:https://stackoverflow.com/questions/9300978/transfer-data-from-javascript-popup-multiline-textbox-to-a-select-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!