pass value to jquery dialog form

风格不统一 提交于 2019-12-02 07:13:46

问题


I need a dialog window to popup with a form and pass a value to this form. I have attempted several approaches to this problem but I have been unable to do so. I have searched all over and nothing seems to work.

This is my latest attempt:

  var message="variable";

 .click(function() {
 $( "#dialog-form" ).dialog( "open" );
 $('<input>').attr({
    type: 'text',
    id: 'hidden-input",
    value: message,
    name: 'bar'
 }).appendTo("#dialog-form");

If i put the append inside the dialog the popup stops working (i think i dont know how to do this properly)


回答1:


I think you're looking for something like this:

<script>
$(function(){
   var message = "variable";

   $("#showDialog").click(function() {
      $("#hidden-input").val(message);

      $("#dialogForm").dialog("open");
   });
});
</script>

<button id="showDialog">Show Dialog</button>

<div id="dialogForm">
<input type="text" id="hidden-input" name="bar"/>
</div>



回答2:


I use script entirely for dialog handling (I think it is cleaner):

http://jsfiddle.net/fE6q2/

html:

Default firstname:<br>
<input type='text' id="default_firstname"/>
<br><br>
<button onclick='on_create_form_clicked()'>create form</button>

javascript:

function on_create_form_clicked()
{
    var html =
       "<div>" +
          "Firstname:<br>" +
          "<input type='text' class='firstname'/>" +
          "<br/><br/>" +
          "Lastname:<br>" +
          "<input type='text' class='lastname'/>" +
          "<br/><br/>" +
          "<button class='ok'>ok</button>" +    
       "</div>";

    var div = $(html);

    var default_firstname = $('#default_firstname').val();
    div.find('.firstname').val(default_firstname);        

    div.find('.ok').click(function()
    {
        div.dialog('close');

        var firstname = div.find('.firstname').val();
        var lastname = div.find('.lastname').val();

        alert("firstname was " + firstname
            + " and lastname was " + lastname);        
    });

    div.dialog(
    {
        title:"Enter firstname and lastname",
        close: destroy_this_dialog
    });
}

function destroy_this_dialog(event, ui)
{
  $(this).dialog("destroy");
  $(this).remove();
}


来源:https://stackoverflow.com/questions/15317953/pass-value-to-jquery-dialog-form

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