¿Por qué jquery .html () no inserta correctamente la primera línea de la lista de selección?

I am trying to apply a validation function to a select option, but I only want to do it if a previous select options value is D

here is the code I am using;

<select name="ord_type" id="ord_type" onchange="chkValue(this)" class="{required:true}" >
        <option value="" selected="selected"></option>
        <option value="D">Delivery</option>
        <option value="CNC">Cash &amp; Carry</option>

    $("#ord_type").change(function () {
          var str = "";
          $("#ord_type option:selected").each(function () {
                str += $(this).val();       
    switch (str) {
    case "D":
        $(".del_txt").html('<select name="del_add_code" size="1" id="del_add_code" class="{required:true}">');
    case "C":
        // do nothing
        // do something if no match is found...
        break; // always break on default to keep consistency
<div class="del_txt"></div>
do {   
<option value='' selected='selected'>Select Delivery Address</option>
      <option value="<?php echo $row_del_add['AC_code']?>"<?php if (!(strcmp($row_del_add['AC_code'], $row_del_add['AC_code']))) ?>><?php echo $row_del_add['AC_Add1'] . " - " . $row_del_add['AC_postcode']?></option>
} while ($row_del_add = mysql_fetch_assoc($del_add));
  $rows = mysql_num_rows($del_add);
  if($rows > 0) {
      mysql_data_seek($del_add, 0);
      $row_del_add = mysql_fetch_assoc($del_add);

The select box's 1st line I am trying to insert has a required validation in it, if the 1st selection's value is CNC I dont want it to apply the validation!

That is why I have set it not to insert the 1st line.

I have also tried inserting all of the rest of the second selection box using the jquery .html() function but I still cant get it to work.

Does anyone have any idea where I am going wrong, or does anyone have any other ideas that would work better?

Muchas Gracias

preguntado el 01 de febrero de 12 a las 22:02

alsot, try console.log($(".del_txt")) to ensure that the object is existing at the time u set the html, and/or try $(".del_txt").append($("<select />").attr({ id:"del_add_code", name:"del_add_code", size:"1" }).addClass('{required:true}')) ... although, what's with the {} around the class name? -

1 Respuestas

whatabout approaching it this way?


Then you could also check your validation, make sure it is getting the list of what to check at the point of submitting...

$('#my_form').submit(function(event) {

  $('.{required:true}').each(function(index) {

     if ($(this).val().length==0){

        alert ('found something that is empty that is required');




Respondido 11 Feb 12, 02:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.