2014-01-07 2 views
0

Я в тупике на что-то немного странное. Я искал какой-то код в html-файле, на котором были js и html, пока я не заработал так, как хотелось бы, и скопировал его в файл php, связанный с JS-страницей. Проблема в том, что на странице php я получаю сообщение об ошибке непризнанного: выберите [name = gateHeight] option: selected. Если бы кто-нибудь мог объяснить, что я испортил, я был бы очень признателен! Вот HTML файл с JS и HTML:JS работает в одном месте, но не в другом

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title></title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#btnAdd').click(function() { 
       var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
       var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

       // create the new element via clone(), and manipulate it's ID using newNum value 
       var newElem = $('#input' + num).clone().attr('id', 'input' + newNum); 

       // manipulate the name/id values of the input inside the new element 
       newElem.children($("select[name=gateHeight] option: selected")).attr('id', 'gateHeight' + newNum).attr('name', 'gateHeight' + newNum); 
       //newElem.children('input[type=text]:first').attr('id', 'name' + newNum).attr('name', 'name' + newNum); 
       newElem.children('input[type=checkbox]:first').attr('id', 'chk' + newNum).attr('name', 'chk' + newNum); 

       // insert the new element after the last "duplicatable" input field 
       $('#input' + num).after(newElem); 

       // enable the "remove" button 
       //$('#btnDel').attr('disabled',''); 
       $('#btnDel').removeAttr('disabled'); 

       // business rule: you can only add 5 names 
       if (newNum == 5) 
        $('#btnAdd').attr('disabled','disabled'); 
      }); 

      $('#btnDel').click(function() { 
       var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
       $('#input' + num).remove();  // remove the last element 

       // enable the "add" button 
       //$('#btnAdd').attr('disabled',''); 
       $('#btnAdd').removeAttr('disabled'); 

       // if only one element remains, disable the "remove" button 
       if (num-1 == 1) 
        $('#btnDel').attr('disabled','disabled'); 
      }); 

      $('#btnDel').attr('disabled','disabled'); 
     }); 
    </script> 
</head> 

<body> 

<form id="myForm"> 
    <div id="input1" style="margin-bottom:4px;" class="clonedInput"> 
     <!-- <input type="text" name="name1" id="name1" /> --> 
     <select name="gateHeight" id="gateHeight"> 
          <option value="select">Select Gate Height</option> 
          <option value="4fg">4 Ft. Gate</option> 
          <option value="6fg">6 Ft. Gate</option> 
          <option value="8fg">8 Ft. Gate</option> 
     </select> 
     Include Spring Pool Latch: <input type="checkbox" name="chk1" id="chk1" /> 
    </div> 

    <div> 
     <input type="button" id="btnAdd" value="add another name" /> 
     <input type="button" id="btnDel" value="remove name" /> 
    </div> 
</form> 
</body> 

А вот JS и HTML другого файла: JS:

//Dynamic Gate Input Fields 
    $('#btnAdd').click(function() { 
       var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
       var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

       // create the new element via clone(), and manipulate it's ID using newNum value 
       var newElem = $('#input' + num).clone().attr('id', 'input' + newNum); 

       // manipulate the name/id values of the input inside the new element 
       newElem.children($("select[name=gateHeight] option: selected")).attr('id', 'gateHeight' + newNum).attr('name', 'gateHeight' + newNum); 
       //newElem.children('input[type=text]:first').attr('id', 'name' + newNum).attr('name', 'name' + newNum); 
       newElem.children('input[type=checkbox]:first').attr('id', 'chk' + newNum).attr('name', 'chk' + newNum); 

       // insert the new element after the last "duplicatable" input field 
       $('#input' + num).after(newElem); 

       // enable the "remove" button 
       //$('#btnDel').attr('disabled',''); 
       $('#btnDel').removeAttr('disabled'); 

       // business rule: you can only add 5 names 
       if (newNum == 5) 
        $('#btnAdd').attr('disabled','disabled'); 
      }); 

      $('#btnDel').click(function() { 
       var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
       $('#input' + num).remove();  // remove the last element 

       // enable the "add" button 
       //$('#btnAdd').attr('disabled',''); 
       $('#btnAdd').removeAttr('disabled'); 

       // if only one element remains, disable the "remove" button 
       if (num-1 == 1) 
        $('#btnDel').attr('disabled','disabled'); 
      }); 

      $('#btnDel').attr('disabled','disabled'); 

HTML:

<div id="input1" style="margin-bottom:4px;" class="clonedInput"> 
         <select name="gateHeight" id="gateHeight"> 
           <option value="select">Select Gate Height</option> 
           <option value="4fg">4 Ft. Gate</option> 
           <option value="6fg">6 Ft. Gate</option> 
           <option value="8fg">8 Ft. Gate</option> 
         </select> 
         Include Spring Pool Latch: <input type="checkbox" name="chk1" id="chk1" /> 
        </div> 

      <div> 
       <input type="button" id="btnAdd" value="Add Another Gate" /> 
       <input type="button" id="btnDel" value="Remove Gate" /> 
      </div> 

ответ

1

Попробуйте с

$("select[name=gateHeight] option :selected") 

:selected является правильным не : selected (без пробела)

ИЛИ

$("select[name=gateHeight] option").filter(":selected") 

Это будет работать

+0

Y'all - это некоторые безумные гении. Лол. Я до сих пор не понимаю, почему он работал в одном файле, но не в другом, но хорошо! – mario

1

You необходимо избавиться от пространства после толстой кишки в option: selected. Просто измените это на option:selected.

+0

Y'all являются некоторые долбанные гении. Лол. Я до сих пор не понимаю, почему он работал в одном файле, но не в другом, но хорошо! – mario

2

Вы пропускаете документ готовый обертку функция, вызываемая вашим кодом перед элементом есть поэтому потертость.

1

Первый вы должны использовать updated version из jquery как JQuery-1,9 или выше.

Второй вы должны использовать newElem.children("select[name=gateHeight]") вместо newElem.children($("select[name=gateHeight] option: selected")) как

newElem.find("select[name='gateHeight']") 
      .attr({'id': 'gateHeight'+newNum, 'name': 'gateHeight' + newNum});// combine attr() using json 

Demo

Смежные вопросы