代码之家  ›  专栏  ›  技术社区  ›  a7md0

jQuery-以特定的分隔形式更改输入值

  •  -1
  • a7md0  · 技术社区  · 7 年前

    我在使用jQuery更改输入值时遇到了一个问题,表单与输入标记分离,因为我使用HTML表(tr、td等…)表单不能在HTML表中。 无论如何,当我试图更改输入值或以相同形式获取输入值时,我得到了“未定义”,以下是完整代码:

    $(document).ready(function() {
      var iUnique = 0;
    
      $("#addSelected").submit(function(event) {
        event.preventDefault();
    
        var mySelectID = $('select[id=mySelect]').val();
    
        $('#forms form:last').after("<form class='ContentForm' id='formContent" + iUnique + "' method='POST'></form>");
        $('#table tr:last').after("<tr><td><input type='text' name='type' value='" + mySelectID + "' form='formContent" + iUnique + "' /></td><td><input type='text' name='Title' value='' form='formContent" + iUnique + "' /></td><td><input type='text' name='Keywords' value='' form='formContent" + iUnique + "' /></td><td><button class='generateKeywords' form='formContent" + iUnique + "'>Keywords</button></td></tr>");
        iUnique++;
      });
    
      $(document).on('click', '.generateKeywords', function() {
        var $form = $(this.form); // get form
        var $inputs = $form.find("input, select, button, textarea");
        var serializedData = $form.serialize(); // actually this for ajax
    
        $inputs.prop("disabled", true); // nothing disabled -- not working
    
        var response = "test, test2"; // this supposed to come from ajax
        $form.find("[name='Keywords']").val(response); // not working
    
        alert('end of listener');
        return false; // disable refresh
      });
    });
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <form id="addSelected" method="POST">
      <select id="mySelect">
    				<option value="0">First</option>
    				<option value="1">Second</option>
    			</select>
      <input type="submit" value="Add" />
    </form>
    
    <div id="forms">
      <form></form>
    </div>
    
    <table id="table">
      <tr>
        <th>Type</th>
        <th>Title</th>
        <th>Keywords</th>
        <th></th>
      </tr>
    </table>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Taplar    7 年前

    $(document).ready(function() {
      var iUnique = 0;
    
      $("#addSelected").submit(function(event) {
        event.preventDefault();
    
        var mySelectID = $('select[id=mySelect]').val();
    
        $('#forms form:last').after("<form class='ContentForm' id='formContent" + iUnique + "' method='POST'></form>");
        $('#table tr:last').after("<tr><td><input type='text' name='type' value='" + mySelectID + "' form='formContent" + iUnique + "' /></td><td><input type='text' name='Title' value='' form='formContent" + iUnique + "' /></td><td><input type='text' name='Keywords' value='' form='formContent" + iUnique + "' /></td><td><button class='generateKeywords' form='formContent" + iUnique + "'>Keywords</button></td></tr>");
        iUnique++;
      });
    
      $(document).on('click', '.generateKeywords', function() {
        // get form name
        var form = this.getAttribute('form');
        // get all the inputs and filter on the form name
        var $inputs = $("input, select, button, textarea").filter(function(){
          return this.getAttribute('form') === form;
        });
        // serialize the found elements
        var serializedData = $inputs.serialize();
        console.log(serializedData);
    
        $inputs.prop("disabled", true); // nothing disabled -- not working
    
        var response = "test, test2"; // this supposed to come from ajax
        $('input').filter("[name='Keywords']").val(response); // not working
    
        //alert('end of listener');
        return false; // disable refresh
      });
    });
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <form id="addSelected" method="POST">
      <select id="mySelect">
    				<option value="0">First</option>
    				<option value="1">Second</option>
    			</select>
      <input type="submit" value="Add" />
    </form>
    
    <div id="forms">
      <form></form>
    </div>
    
    <table id="table">
      <tr>
        <th>Type</th>
        <th>Title</th>
        <th>Keywords</th>
        <th></th>
      </tr>
    </table>