jquery中的初始化对话框标签

时间:2013-06-04 22:51:15

标签: javascript jquery html

我正在尝试将一些文本添加到jquery对话框中,但我无法使用某些文本初始化字段。这里有一些html:

<div id="dialog-form-obiectEd" title="Create new userdasdasdasd">
  <p class="validateTips">All form fields are required.</p>

  <form>    
  <fieldset>
    <label for="nameOE">Nume</label>
    <input type="text" name="nameOE" id="nameOE" class="text ui-widget-content ui-corner-all" />
    <label for="descriereOE">Descriere</label>
    <input type="text" name="descriereOE" id="descriereOE" value="" class="text ui-widget-content ui-corner-all" />
    <label for="proprietatiOE">Proprietati</label>
    <input type="text" name="proprietatiOE" id="proprietatiOE" value="" class="text ui-widget-content ui-corner-all" />
  </fieldset>
  </form>
</div>

这是我点击图片时调用的函数:

function openItem(obiect){

$('#nameOE').empty();
$('#nameOE').text("some value");


$( "#dialog-form-obiectEd" ).dialog( "open" );

reparaZindex();
}

对话框是这样的:

$(function() {
    var name = $( "#nameOE" ),
      descriere = $( "#descriereOE" ),
      proprietati = $( "#proprietatiOE" ),
      allFields = $( [] ).add( name ).add( descriere ).add( proprietati ),
      tips = $( ".validateTips" );


    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
        return false;
      } else {
        return true;
      }
    }

    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }

    $( "#dialog-form-obiectEd" ).dialog({


      autoOpen: false,
      height: 400,
      width: 400,
      modal: true,
      buttons: {
        "Create an account": function() {

          var bValid = true;
          allFields.removeClass( "ui-state-error" );

          bValid = bValid && checkLength( name, "nume", 1, 100 );
          bValid = bValid && checkLength( descriere, "descriere", 1, 100 );
          bValid = bValid && checkLength( proprietati, "proprietati", 4, 100 );

          bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
          // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
          bValid = bValid && checkRegexp( descriere, /^[a-z]([0-9a-z_ \ ])+$/i, "eg. ui@jquery.com" );
          bValid = bValid && checkRegexp( proprietati, /^(([0-9a-zA-Z!\?])+\:([0-9a-zA-Z!\?])+\,?)+$/, "Descrierile trebuie sa contina doar litere si cifre ,:!?, separate prin punct si virgula" );

          if ( bValid ) {

        /////////////////////


        editItem(name.val(),descriere.val(),proprietati.val());


            $( this ).dialog( "close" );
          }
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },
      close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
      }
    });

    $( ".obiectPlus" )
      .button()
      .click(function() {
        $( "#dialog-form-obiectEd" ).dialog( "open" );
      });
  });

我希望nameOE在对话框打开时带有一些字符串。

1 个答案:

答案 0 :(得分:1)

替换......

$('#nameOE').empty();
$('#nameOE').text("some value");

...与......

$('#nameOE').val("some value");

...因为它是一个输入,您想要更新其,而不是其文本内容。