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)
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>
I use script entirely for dialog handling (I think it is cleaner):
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