admin管理员组

文章数量:1429061

I have code that displays a table row with 2 inputs styled using jquerymobile.

I'm using jquery to dynamically add rows when a "plus" button is clicked.

The problem:

When I append a row the static items retain their styling, but the appended row isn't styled.

What am I doing wrong?

Here's the code: /

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href=".2.1/jquery.mobile-1.2.1.min.css" />
<script src=".8.3.min.js"></script>
<script src=".2.1/jquery.mobile-1.2.1.min.js"></script>
<script type="text/javascript" src=".min.js"></script>
</head>
<body>
<div data-role="page">
  <div data-role="header">
    <h1>Test</h1>
  </div>
  <!-- /header -->

  <div data-role="content">
    <div class="input_fields_wrap">
      <table width="100%" border="0" cellspacing="5" cellpadding="5">
        <tr>
          <td width="180px">Entry price</td>
          <td>Percent of position</td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
      </table>
    </div>
    <div align="left">
      <button class="add_field_button" data-icon="plus" data-inline="true" >Add</button>
    </div>
    <!-- When "Add" is clicked, execute python script --> 

  </div>
  <!-- /content --> 

</div>
<!-- /page --> 

<script language="javascript">
<!-- adds or deletes input fields dynamically -->
$(document).ready(function() {
    var max_fields      = 1000; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID   
    var addline         = '<tr>\
                             <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>\
                            <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>\
                            </tr>';

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append(addline); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>
</body>
</html>

I have code that displays a table row with 2 inputs styled using jquerymobile.

I'm using jquery to dynamically add rows when a "plus" button is clicked.

The problem:

When I append a row the static items retain their styling, but the appended row isn't styled.

What am I doing wrong?

Here's the code: http://jsfiddle/Trident/o9r4csc9/

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery./mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
<script src="http://code.jquery./jquery-1.8.3.min.js"></script>
<script src="http://code.jquery./mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery./jquery.min.js"></script>
</head>
<body>
<div data-role="page">
  <div data-role="header">
    <h1>Test</h1>
  </div>
  <!-- /header -->

  <div data-role="content">
    <div class="input_fields_wrap">
      <table width="100%" border="0" cellspacing="5" cellpadding="5">
        <tr>
          <td width="180px">Entry price</td>
          <td>Percent of position</td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
        <tr>
          <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>
          <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>
        </tr>
      </table>
    </div>
    <div align="left">
      <button class="add_field_button" data-icon="plus" data-inline="true" >Add</button>
    </div>
    <!-- When "Add" is clicked, execute python script --> 

  </div>
  <!-- /content --> 

</div>
<!-- /page --> 

<script language="javascript">
<!-- adds or deletes input fields dynamically -->
$(document).ready(function() {
    var max_fields      = 1000; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID   
    var addline         = '<tr>\
                             <td width="180px"><input type="number" name="entryprice" size="5" min="0" placeholder="100"></td>\
                            <td><input type="range" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" /></td>\
                            </tr>';

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append(addline); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>
</body>
</html>
Share Improve this question edited Nov 14, 2014 at 8:35 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Nov 14, 2014 at 8:35 SageSage 931 silver badge8 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

In JQuery mobile you can manually update dynamic content by the following code:

  $(wrapper).trigger('create');  

Just one line and all done.

Note: You have to add jquery script for this. I have added it + the css. Please update the UI issue by your self. The point is that the style is working.

http://jsfiddle/o9r4csc9/9/

Happy coding :)

In the fiddle you are appending the new table rows into the containing div rather than the table. Change your wrapper variable to reference the table instead.

var wrapper = $(".input_fields_wrap table"); //Fields wrapper

Your rows are recieving jquery-ui styling dynamically, which you haven't included in your 'addrow' html. A better way would be to clone another row's html, in your case the best would be the last row.

var addline = wrapper.find('tr').last();

And then change the appending line:

$(wrapper).append(addline.clone()); //add input box

I changed a couple of lines in your code:

The first one to append the table row to the table, not the wrapping div

var wrapper         = $(".input_fields_wrap table"); //Fields wrapper

And the second one to get the right HTML, now and in the future

var addline         = wrapper.find('tr:last')[0].outerHTML;

I have modify your html as required at

    http://jsfiddle/o9r4csc9/6/
Changes :
1. ID at table <br>
2. Append Row to Table not at div <br>
3. Few css added <br>

i have modified your script section and test on http://jsfiddle/o9r4csc9/12/ please change your code as below

$(document).ready(function() {
    var max_fields      = 1000; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID   
    var addline         = '<tr>\
                             <td width="180px"><input class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" name="entryprice" size="5" min="0" placeholder="100" type="number"></td>\
                            <td><input class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-slider-input" name="slider-fill" id="slider-fill" value="10" min="0" max="100" data-highlight="true" data-type="range" type="number"><div class="ui-slider  ui-btn-down-c ui-btn-corner-all" role="application"><div style="width: 10%;" class="ui-slider-bg ui-btn-active ui-btn-corner-all"></div><a style="left: 10%;" aria-labelledby="slider-fill-label" title="10" aria-valuetext="10" aria-valuenow="10" aria-valuemax="100" aria-valuemin="0" role="slider" data-theme="c" data-wrapperels="span" data-iconshadow="true" data-shadow="true" data-corners="true" class="ui-slider-handle ui-btn ui-btn-up-c ui-shadow ui-btn-corner-all" href="#"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text"></span></span></a></div></td>\
                            </tr>';

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).find("table").append(addline); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

本文标签: javascriptLosing Styling When Dynamically Adding New Items OnclickStack Overflow